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!
VivienneNAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VivienneNAuthor Commented:
Had to do some readup
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.