• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

How do I use fgetc to read from a text file and separate the text in it?

I have been trying to take a list of text from a file, and is not really sure on how to do that.

Currently it is delimited by ',' and '.'

I want to take out all the characters between '.' and later characters by ',', and I have no idea how many characters there may be. Eventually I need to print it out through just one string and I have to give it a newline in between.

eg.
tom and jerry,collection.peter pan,rental.

Will I need to create many many char types to store them all and to finally add it to one variable for my formatting?


I have been up all day and night trying all kinds of stuff but I keep getting stuck.

I am new to C++, so I would appreciate any detail explanation. Thanks!
0
VivienneN
Asked:
VivienneN
  • 2
  • 2
1 Solution
 
jkrCommented:
For 'fgetc()', tht would be like
include <stdio.h>
#include <stdlib.h>

int main( void )
{
   FILE *stream;
   char buffer[81];
   int  i, ch;

   // Open file to read line from:
   stream = fopen( &stream, "fgetc.txt", "r" );
   if( stream == NULL )
      exit( 0 );

   // Read in first 80 characters and place them in "buffer": 
   ch = fgetc( stream );
   for( i=0; (i < 80 ) && ( feof( stream ) == 0 ); i++ )
   {
      buffer[i] = (char)ch;
      ch = fgetc( stream );
   }

   // Add null to end string 
   buffer[i] = '\0';
   printf( "%s\n", buffer );
   fclose( stream );
}

Open in new window


Then you can use 'strtok()' to plit up the line:
#include <string.h>
#include <stdio.h>

char string[] = "tom and jerry,collection.peter pan,rental.";
char seps[]   = " ,.";
char *token;

int main( void )
{
   printf( "Tokens:\n" );
 
   // Establish string and get the first token:
   token = strtok( string, seps ); // C4996
   // Note: strtok is deprecated; consider using strtok_s instead
   while( token != NULL )
   {
      // While there are tokens in "string"
      printf( " %s\n", token );

      // Get next token: 
      token = strtok( NULL, seps ); // C4996
   }
}

Open in new window


See also

http://www.cplusplus.com/reference/clibrary/cstdio/fgetc/
http://www.cplusplus.com/reference/clibrary/cstring/strtok/
0
 
VivienneNAuthor Commented:
Thanks, but since they are all in chars, how can I store them in blocks of chars and still manage them one group by one group? I split them up, but essentially I am just removing the delimiter and I cannot do any formatting. I need to make it as follows and yet still have the flexibility to change it however I want: (there is also price and I need to do comparison)

1. peter pan<tab>-<tab>rental<tab><tab>Cost($): 19<\n>
2. tom and jerry<tab>-<tab>collection<tab><tab>Cost($): 9<\n>


This is getting frustrating! :(
0
 
jkrCommented:
You should consider putting that in the loop that isolates the parts in between the tokens, e.g.
int index = 0;

   while( token != NULL )
   {
      // While there are tokens in "string"
      printf( " %s\n", token );

      // depending on the value of 'index', orocess 'token' accordingly

      index++;
      // Get next token: 
      token = strtok( NULL, seps ); // C4996
   }

Open in new window

0
 
Deepu AbrahamR & D Engineering ManagerCommented:
Another way of doing it using strchr() function.
char szCharArray[] = "tom and jerry,collection.peter pan,rental.";

int main( void )
{


    char* p = NULL;
    char* pCharArray = NULL;
    char szformat[256];

    p=strchr(szCharArray,'.');
    pCharArray = szCharArray;

	while(p!= NULL)
	{

			*p = '\0';			

			char* tmp = strchr(pCharArray,',');
			if(tmp)
				*tmp='\t';

			sprintf(szformat,"%s\tCost($): %d\n",pCharArray,25);
			printf("%s\n",szformat);


			p=p+1;
			pCharArray = p;
			if(pCharArray)
			   p=strchr(pCharArray,'.');

	}

	return 0;
}

Open in new window

0
 
VivienneNAuthor Commented:
Had to do some readup
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now