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

Recommended Answers

All 2 Replies

Check the result of scanf(). Maybe the last line of the file is a blank line.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.