PDA

View Full Version : Problem with premature end of loop


singularity2006
03-05-2004, 06:55 AM
I'm trying to write a block of code in C to parse an input text file with nothing but spaces and floating point numbers to count the number of columns in each row (or the number of floats in each row). What I have so far gives me 1 extra column in the counter for row 1 and seems to skip row 5 altogether.

HEre is the input file:


1.0 2.1 16.7 8.5
2.0 3.4
3.0 2.0 6.0
4.0 1.0
5.0



void count_columns(FILE*input, FILE*output)
{
//LOCAL DEFINITIONS
char scanned_char;
float scanned_float;
int column_count = 0;
int row_count = 0;

//COLUMN COUNT PER LINE
while((scanned_char = fgetc(input)) != EOF)
{
if(scanned_char != ' ' && scanned_char != '\t' && scanned_char != '\n')
{
fscanf(input,"%f", &scanned_float);
column_count++;
}

if(scanned_char == '\n')
{
row_count++;
fprintf(output,"Row %d of the input file has %d column(s).\n", row_count, column_count);
column_count = 0;
}
}
printf("Column counts per row have been recorded to log.\n");
return;
}

my output is as follows:

Row 1 of the input file has 4 column(s).
Row 2 of the input file has 2 column(s).
Row 3 of the input file has 3 column(s).
Row 4 of the input file has 2 column(s).

Why did it skip row 5?

iC4
03-05-2004, 08:13 AM
because in the last line after 5.0 there is no "\n", so you skip the last if and because of that there is no output

Hugh
03-05-2004, 08:58 AM
Some text editors automatically make sure there is a newline character at the end of a file, but if it is your code that has created the file, then this might not happen...

Best thing (in your case) to do is, after the loop finishes, check if column_count > 0 and if so, print out the information for the last row...


I don't quite get why row 1 has one extra column... it looks okay to me...

singularity2006
03-05-2004, 04:53 PM
Hmmm... I fixed the problem with column 1 incrementing one over what it should. I'm running into the skipping of row_increment++ for the 5th line because of the condition I have. I'm having trouble writing a condition to account for that. The current condition is:


if(scanned_char == '\n')
{
row_count++;
fprintf(output,"Row %d of the input file has %d column(s).\n", row_count, column_count);
column_count = 0;
}


There is no new line character there. At the same time, if EOF is reached at the end of the 5th line, the loop doesn't run anyway. Can anyone suggest a different way of writing the condition? Currently, I just took a shortcut and at the very end after the loop ends, I have row_count++ automatically done and appended to file. It works, but I would prefer it exist within the loop. But if that can't be helped, oh well.

By the way, I was writing this particular function under the assumption that the input file contained only white spaces and float numbers. Would it require much more code if I had to write it under the assumption that I had absolutely no clue what was in the file?

Hugh
03-05-2004, 05:40 PM
I think you might have missed what I said in my post before yours....

After the loop finishes, check the value of column_count.

If it is 0, then there wasn't a last row - otherwise there was, with as many columns as the value...

singularity2006
03-06-2004, 05:41 AM
yah, i made a couple of adjustments after reading over ur post. Anyhow, I realized it was incrementing prematurely so I had to change conditions around. The only thing I didn't quite like was now that it's done, it reads up to row 5 and increments the column count fine, but refuses to increment the row count. So I'm in essence, short 1. Anyway, I'm done trying to figure it all out. This lab has taken up way too much of my time in the past 3 days. But much thanks for all your input. Here's the source code for what I ended up using:


void count_columns(FILE*input, FILE*output)
{
//LOCAL DEFINITIONS
char scanned_char;
float scanned_float;
int column_count = 0;
int row_count = 0;

//COLUMN COUNT PER LINE
while((scanned_char = fgetc(input)) != EOF)
{
if(scanned_char != ' ' && scanned_char != '\t' && scanned_char != '\n')
{
fscanf(input,"%f", &scanned_float);
column_count++;
}
else if(scanned_char == '\n')
{
row_count++;
fprintf(output,"Row %d of the input file has %d column(s).\n", row_count, column_count);
column_count = 0;
}
}

//ACCOUNTING FOR EOF LOOP BREAK FOR row_count++
if(row_count != 0);
{
row_count++;
fprintf(output,"Row %d of the input file has %d column(s).\n", row_count, column_count);
}

//END FUNCTION
printf("Column counts per row have been recorded to log.\n");
return;
}

CGTalk Moderation
01-17-2006, 03:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.