0

I'm writing a program to read data and store it to file. However I am a bit confused by the EOF statement, and how to get my program to stop reading when there is no more data to read.

My code looks like this

while((c=fgetc(fin2))!=EOF) {
								
	if(c=='#'){
								
		while((c=fgetc(fin2))!='\n'){
	}}
								
	else{
		ungetc(c,fin2);
								
		fscanf(fin2, "%lf\t %lf\t %lf\t %lf\t %lf)\n", &V2[j], &T2[j], &N4, &N5, &N6);
		
		fprintf(fout2, "%d	 %lf		%lf\n",j, V2[j], T2[j]);
		if(T2[j]>MaxT2){
					MaxT2=T2[j];
					MaxV2=V2[j];
			}
		
		++j;
		
		}
		}

However, it prints an extra line of "nans"
Any help would be amazing
thank you

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by WaltP
0

You are making things very hard for yourself. Just use the scanf() and check the return value. You don't need the ungetc() .

Other changes:
Get rid of the \t's in the scanf() format. They aren't needed.
Exit the loop when the return from scanf() is not as expected
And format your code. It's really bad and hard to follow.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.