Reputation: 11
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
Upvotes: 0
Views: 365
Reputation: 153338
Checking the return value from fscanf()
against the expected value, not against EOF
is a good idea. @BLUEPIXY @Peter. This will identify/solve most of OP's problems.
The data file has keywords in it, they needed to be scanned too. @user3386109
Suggest direct code to read the entire record with one fscanf()
.
struct kk {
int time;
int switchn[4];
double potentiometer, temperature, light;
};
int data_kk_read(FILE *inf, struct kk *data) {
int cnt = fscanf(inf,
" time: %d ms switch0: %d switch1: %d switch2: %d"
" switch3: %d potentiometer: %lf temperature: %lf light: %lf"
" --------------------------- ",
&data->time,&data->switchn[0],&data->switchn[1], &data->switchn[2],
&data->switchn[3], &data->potentiometer, &data->temperature, &data->light);
return cnt;
}
int data_kk_write(FILE *outf, unsigned n, const struct kk *data) {
int cnt = fprintf(outf,
"%3u %d, %d, %d, %d, %d, %.2lf, %.2lf, %.2lf\n", n,
data->time, data->switchn[0], data->switchn[1], data->switchn[2],
data->switchn[3], data->potentiometer, data->temperature, data->light);
return cnt;
}
int main(void) {
FILE *inf = fopen("data.txt", "r");
assert(inf);
unsigned line = 0;
struct kk data;
int cnt;
while ((cnt = data_kk_read(inf, &data)) == 8) {
data_kk_write(stdout, ++line, &data);
}
fclose(inf);
if (cnt != EOF) puts("Unexpected scanning problem");
return 0;
}
Output
1 40, 1, 1, 1, 1, 0.00, 0.66, 0.23
2 80, 1, 1, 1, 1, 0.00, 0.66, 0.23
3 120, 1, 1, 1, 1, 0.00, 0.66, 0.23
Upvotes: 2