Solved

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

Posted on 2011-03-12
5
482 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

937 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

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now