Problem reading a text file

Trying to read a text file file but I have been running into troubles. Below is the content of the file that I am using

0.001
0.002
0.003
0.004
0.005

The file was opened properly but reading it was a completely different story; here is the relevant portion of the code that I use to read the above file

while (fgets(buf,sizeof(buf), fp)!=NULL){

sscanf(buf,"%lf",&nT);
printf("%lf \n",nT);

}

The output:
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.002000
0.003000
0.004000
0.005000
0.005000

This has been driving me crazy, I have no clue what is going on.
Any help will be appreciated
Thanks
If your data file containing the numbers looks like this - with blank lines at the start of the file





0.001
0.002
0.003
0.004
0.005


Then there can be problems because the blank lines (or possibly other 'funny' characters) will give a readings of 0.0000
Last edited on
guestgulkan, thanks a lot!

I really made sure NOT to leave blank lines or any other characters at the start of the file actually, but the output was weird still.
Is it possible that there might be hidden characters within the file that might affect the reading? And if so, how can we eliminate them?
Last edited on
Post your code let's have a look - the code should be straight forward - so I wouldn't be surprised if the data file is the problem ( maybe it's not an absolutely plain text file).

I would say - make a new text file using a plain text editor (are you on windows then use notepad) so we are sure we have plain text.

but post your code anyway.
Last edited on
Here is the complete code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
	
[code]
int main (void) {
    	 	
	 char file[] = "irrevers.txt";
	 char buf[4096];
	 FILE *fp;

	 double nT;
	 //double *DataNQ;
	 
	 if((fp = fopen(file, "r")) == NULL)
	 {
		 printf("fopen %s failed!\n", file);
		 exit(1);
	 }
	 
	while (fgets(buf,sizeof(buf), fp)!=NULL) 
	{
		sscanf(buf,"%lf",&nT);
		printf("%lf \n",nT);
	}

	fclose(fp);
	
    return 0;
}
[/code]

I am using the gcc compiler on Mac actually, the text file was created using Mac TextEdit, the outcomes were exactly the same whether I used plain text format or the rich text format (irrevers.rtf). My ultimate goal here is to store the values from the input file into the array DataNQ
Thanks
Last edited on
I too use GCC, and your program (which was pretty much the same as mine with the exception of the file name) worked exactly as expected with no
problems at all.

I still think it is your data file though - maybe someone using a Mac TextEdit have an explanation.

I'm somewhat intrigued - I'll see if Mac TextEdit is has a port for windows - and if so I'm going to download it ans see if there is an issue there.
Last edited on
Got it finally, and you are absolutely right that the issue was with Mac TextEdit. I used Windows Notepad to create the file from scratch and it works perfectly now.

Thanks a lot for the insight, it was really helpful and appreciated!
Topic archived. No new replies allowed.