c++ file read line per line

c++ file read line per line, dos not work

Page 1 of 1

2 Replies - 5172 Views - Last Post: 15 November 2010 - 11:31 AM Rate Topic: -----

#1 zigurdis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 05-November 09

c++ file read line per line

Posted 15 November 2010 - 11:02 AM

Hello, i need read from file, this is part of program witch not work.
I have defined txt file first line is integer, in second line is char number number,
when i read one line its work ok, bet when 2 or more it not...
can someone can help me? i need to read from file and then do somethink...but thats work;)
i have problem with reading part


#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"

int main(void)
{
	FILE* inFile;
	FILE* outFile;


	char aString[101]; 
char a[]= "text";
char koks[10000];

	inFile = fopen("inorder.in", "r");  
	outFile = fopen("inorder.out", "w+");


	while( fscanf( inFile, "%[^\n]", aString )!=EOF ) {
    // fscanf( inFile, "%[^\n]", aString );

      if   ( isdigit(aString[0]) ) {
           koks[0]=aString[0];
           fprintf(stdout, "%s\n", a);  
            fprintf(stdout, "%s\n", koks); 
           };



           if (aString[0]=='R') 
                       {       
                           
                                 
                      if   (aString[1]==' ')
                       if   ( isdigit(aString[2]) )
                         if   (aString[3]==' ')
                            if   ( isdigit(aString[4]) )
                          fprintf(stdout, "%s\n", a);
                           
                         
                         
  
                         }
                         
                         
                      
    
                
                       }

                
                




	fclose(inFile);
	fclose(outFile);
	  system("pause");

	return 0; 
}




Is This A Good Question/Topic? 0
  • +

Replies To: c++ file read line per line

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: c++ file read line per line

Posted 15 November 2010 - 11:31 AM

Quote

c++ file read line per line


If this should be C++ then you should be using the iostreams to read the file not "C" file io.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5821
  • View blog
  • Posts: 12,674
  • Joined: 16-October 07

Re: c++ file read line per line

Posted 15 November 2010 - 11:31 AM

Try fgets instead. The example should get you going.

Check the length before you even bother processing the line, that will avoid accessing bits you don't want to.

One you have the line, that's when you might want to play with scanf, or rather sscanf.

If that's overkill, I'd write your check like this:

if (aString[0]=='R' && aString[1]==' ' && aString[3]==' ' && isdigit(aString[2]) && isdigit(aString[4])) {
}
/* why do you always print "text" here?


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1