Solved

Reading Input From A File

Posted on 1998-11-20
5
199 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
[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
  • 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

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

751 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