Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-03-12
5
Medium Priority
?
515 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:
Deepu Abraham earned 1500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

609 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