I am trying to convert an input file from .txt to .csv. I've performed multiple tests using gdb and switched my code around. The code looks like it should work but for some reason it doesn't. I've tried using "while (fscanf(…arguments…) != EOF)" but I always end up in a never-ending loop when I know that the input file does end. Is it the way I'm trying to read the file that's the problem or something else? I'd greatly appreciate any advice.
A sample of the input file (it's way too big. Also the potentiometer value is the only value that is consistently zero. All other values are greater than zero)
time: 40 ms
switch0: 1
switch1: 1
switch2: 1
switch3: 1
potentiometer: 0.00
temperature: 0.66
light: 0.23
---------------------------
time: 80 ms
switch0: 1
switch1: 1
switch2: 1
switch3: 1
potentiometer: 0.00
temperature: 0.66
light: 0.23
---------------------------
time: 120 ms
switch0: 1
switch1: 1
switch2: 1
switch3: 1
potentiometer: 0.00
temperature: 0.66
light: 0.23
---------------------------
The file that convert from txt to csv
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6 FILE *data = fopen("data.csv","w");
7 FILE *arduino = fopen("arduino.txt","r");
8
9 if(arduino == NULL)
10 {
11 printf("error reading file\n");
12 return 1;
13 }
14 if(data == NULL)
15 {
16 printf("error writing file\n");
17 return 2;
18 }
19
20 fprintf(data,"Time,Switch0,Switch1,Switch2,Switch3,Potentiometer,Temperature,Light\n");
21
22 int num1,num2,num3,num4,num5;
23 double num6,num7,num8;
24
25 double temp1[800];
26
27 int count1 = 0;
28
29 while(count1<800)
30 {
31 fscanf(arduino,"%lf",&temp1[count1]);
32 count1++;
33 }
34
35 for(count1 = 0; count1 < 800; count1++)
36 {
37 printf("%lf",temp1[count1]);
38 }
39
40
41 int count2 = 0;
42 int i = 0;
43
44 while(count2 != 800)
45 {
46 for(i=0 ; i <8;i++)
47 {
48 if(i==7)
49 {
50 fprintf(data,"%lf\n",temp1[count2]);
51 }
52
53 else
54 {
55 fprintf(data, "%lf,", temp1[count2]);
56 }
57 count2++;
58 }
59 }
60
61
62 if (fclose(arduino)==EOF)
63 {
64 printf("error closing input file\n");
65 }
66 if(fclose(data)==EOF)
67 {
68 printf("error closing output file\n");
69 }
70 }
and here's the output
Time,Switch0,Switch1,Switch2,Switch3,Potentiometer,Temperature,Light
2 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
3 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
4 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
5 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
6 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
7 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
8 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
9 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
(still zeros across the board)
61 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
62 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
63 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
64 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
65 0.000000,0.000000,0.000000,0.000000,-nan,0.000000,0.000000,0.000000
66 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
67 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
68 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
69 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
70 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
71 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
72 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
73 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
74 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
75 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
76 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
77 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
78 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
79 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
80 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
81 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
82 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
83 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
84 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
85 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
86 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
87 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
88 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
89 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
90 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
91 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
92 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
93 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
94 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
95 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
96 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
97 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
98 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
99 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
100 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,253700437344304240814662650531587413567701629019053044079803999006261210888228446189339915094527360 92923426425002851172634311446770729875573243622981632.000000,49038509684686202755808411764574575003743211375155249005916427827780247417991687082747214451 073341675744581253991867335918252416362555908299070786942125737694751726823604090062182039519355613866611467434357822207669472484839486934106348907556279 40839424.000000
101 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000
fscanf. Reading numerical values will fail if there is a character string. - BLUEPIXYtime:), a number (40), and another string (ms). So the firstfscanfneeds to use a format string like"%*s%lf%*s". Note thatfscanfisn't going to skip the strings, unless you tell it to. - user3386109fscanf()returns 0 when it can't match anything — it only returns EOF when there is no data left. It does not discard unrecognizable data. - Jonathan LefflerEOF,fscanf()returns the number of values it has successfully read. That number can be less than the number specified in the format string if an error occurs. Depending on what error has occurred (e.g. encountering a character like'Q'when the format string specifies afloat) looping onfscanf != EOFcan yield an infinite loop, unless other checks are done. - Peter