Reading Input From A File

I am in need of some help.

I have to take data from a file and  am having trouble finding the best way to do it.

The file contains polynomial transactions.  Eachhh Transaction is seperated by a new line character.   When you hit EOF you know you are done having to read from the file

Here isan exammple of a file tat as two polynomial transactions

(x - 2) * ( x - 2 ) - ( x2 + 3) + ( 4x2 -8) * d( 2x+ 1)
(x+2)*(x+ 4)

**Notice the difference in how the spacing is.  The spacing can always be different.  

Any help.
larockdAsked:
Who is Participating?
 
emmonsConnect With a Mentor Commented:
Horrible is a bit strong.
So, you need a function to read in a line, and report when you get to EOF:
/* returns # chars in line, takes buffer and max length */
int getline( char s[] /*data*/, int lim /* max_line_length*/)
{
  int c, i
  i = 0;
  while( --lim > 0 && (c=getchar()) != EOF && c != '\n') {
     if( c != ' ' && c != '\t')  /* strip out pesky blanks */
       s[i++] = c;
  }
  if( c == '\n')
    s[i++] = c;
  s[i] = '\0';
  return( i);
}


Once you have the line,
0
 
NorbertCommented:
It looks like a homework. Isn't it?
I think we should not do the work for you
But some tips:
There is a function fgets that reads from a file until the newline occures.
Great to read a line without having attention about the spacing.
when you have read the line you can modify it to eleminate the spacing - without spaces they will not have to be interpeted.
do what you have to do with the line - I think you have to calculate - and after that get the next line
there is a feof function that tells you if you reach the end of file
now you know how in principal I would do it and I hope that will help you
If you have further questions with any details of the implementation feel free to ask an other question
0
 
larockdAuthor Commented:
Yes,it is homeworkand I have made an attempt to get it to work.  fgets requires you to know the size to read.  Each transaction is a random length.  I am able to read the first line in ok, but getting subsequent transactions I am not able to get it to work for.


0
 
larockdAuthor Commented:



    /* Determine Length and Terms */

    char* pData = NULL ;
      int iTerm = 0 ;
      int iLength = 0 ;
      char c;

      /* Determine Length Of The Polynomial String And Terms Incolved */

      while ( ( c = fgetc( pfInfile ) ) != '\n' )
      {
            #ifdef DEBUG
               printf("%c",c);
            #endif

            if ( c == ')' )
                  iTerm++;
            iLength++;
      }
      iLength++;  /* Increases Length Because fgets reads n - 1 */

      fseek( pfInfile, 0L, SEEK_SET );

      pData = ( char * ) malloc ( sizeof(char) * iLength ) ;
      fgets( pData, iLength, pfInfile) ;

    #ifdef DEBUG
         printf("\nLength = %d \n Terms = %d \n", iLength, iTerm);
         printf("%s \n",pData);
      #endif






This is how I was doing it, but as you can see it only works for oneline does not handle eof condition and does not work when trying to get second or thierd transactions.

I feel what I am doing above is horrible, and I am in a rut having a hard time to get out.



0
 
larockdAuthor Commented:
thank oyu
0
All Courses

From novice to tech pro — start learning today.