Solved

Reading Input From A File

Posted on 1998-11-20
5
198 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reading tzdatabase for timezone definitions 5 145
undefined reference to `bswap_128' 9 175
Global Keyboard Hooks Blocked 4 81
Fill Null values 5 29
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

820 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