Basically I am trying to read a "block" of data from a file that looks something like
000000000
000000001
000100000
100000000
000000000
000000000
000000000
The above example would translate into TWO blocks of data. First one is 4 lines tall, second one is 3 lines tall.
The double spaces separates the "blocks".
Here is my code to read until double space is encountered. (This reads the first loop, I then use fseek
to read whatever other block I want to read - it doesn't really matter in this question though)
while(!endofblock){
firstchar = fgetc(fptr); //read the first character
if(firstchar == '\n'){
if(fgetc(fptr) == '\n'){ //if its newline AND newline
endofblock = 1; //end of block is reached, loops break
}else{ //if its just one new line
*h = *h + 1; //increment h (height), a pointer passed from elsewhere
}
}
if(feof(fptr)){
endofblock = 1;
}
}
This works fine most of the time - but there are complex unexplainable cases that it doesn't work. Plus, it is very bloated and messy.
Is there a neater, better way to count the number of new line
and stop counting when double new line
is reached?