Solved

Reading Input From A File

Posted on 1998-11-20
5
195 Views
Last Modified: 2012-05-04
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.
0
Comment
Question by:larockd
  • 3
5 Comments
 
LVL 3

Expert Comment

by:Norbert
ID: 1254503
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
 

Author Comment

by:larockd
ID: 1254504
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
 

Author Comment

by:larockd
ID: 1254505



    /* 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
 
LVL 4

Accepted Solution

by:
emmons earned 50 total points
ID: 1254506
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
 

Author Comment

by:larockd
ID: 1254507
thank oyu
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

757 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

20 Experts available now in Live!

Get 1:1 Help Now