Hey guys,
I have a silly issue that is bothering me. I am trying to read an unknown amount of lines from an input file and basically stop when I reach the EOF. I probably does'nt matter but the assignment is to create a simple lexical analyzer/scanner for a mini-subset of Pascal. Long story short, I need to read character at a time until either my buffer is full or I reach the end-of-line symbol. Because I have gone so far in my program, many of my functions a designed to rely on the inclusion of '\n' at the end of my buffer. Here is some code that will maybe assist you in understanding what I mean
Line ReadFile(FILE *in)
{
struct Line inputLine;
int loopControl=0;
char c;
//reading data into buffer
c = fgetc(in);
if( (int)c == 10 )
c=fgetc(in);
while(loopControl < 80 && (int)c != 13)
{
if((int) c == 10)
{
inputLine.lineArray[loopControl]= c;
loopControl= loopControl + 1;
break;
}
inputLine.lineArray[loopControl]= c;
c = fgetc(in);
loopControl=loopControl + 1;
}//end of while loop
//setting the actual size of the array
inputLine.lineArraySize = loopControl;
return inputLine;
}
The structure Line looks like this:
struct Line
{
char lineArray[80];
int lineArraySize;
};
The problem is that I read everything in the file correctly, but when I get to the last line in the file the data on that line is skipped. This is true if I use a looping mechanism something like this:
while( feof(inputFile)==0 )
{
bufferInput= ReadFile(inputFile);// read a buffer-worth of data from input file
clearBuffer(noCommentBuffer);//JUST CLEARS BUFFER.
}
and if I try to modify the looping mechanism to control the reading process to something like the following it will read the last line of data but it will not properly fill the data in the buffer.
bufferInput= ReadFile(inputFile);// read a buffer-worth of data from input file
while( feof(inputFile)==0 )
{
//process data
clearBuffer(noCommentBuffer);//JUST CLEARS BUFFER.
bufferInput= ReadFile(inputFile);// read a buffer-worth of data from input file
}
Can anyone please help me. This scanne project is hard in general, but I understand it. This I/O stuff in C I am not getting.
Thanks for any advise
Alex