Solved

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

Posted on 2011-03-12
5
491 Views
Last Modified: 2012-05-11
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
Comment
Question by:VivienneN
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 35116854
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
 

Author Comment

by:VivienneN
ID: 35120237
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
 
LVL 86

Expert Comment

by:jkr
ID: 35122309
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
 
LVL 11

Accepted Solution

by:
DeepuAbrahamK earned 500 total points
ID: 35122512
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
 

Author Closing Comment

by:VivienneN
ID: 35171190
Had to do some readup
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question