我正在尝试将输入文件从.txt转换为.csv。我使用gdb执行了多次测试并切换了我的代码。代码看起来应该可以工作,但由于某种原因它不会。我尝试过使用“while (fscanf(…arguments…) != EOF)
”但是当我知道输入文件结束时,我总是以一个永无止境的循环结束。这是我试图读取文件的方式是问题还是其他什么?我非常感谢任何建议。
输入文件的样本(它太大了。电位器值也是唯一一直为零的值。所有其他值都大于零)
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
---------------------------
从txt转换为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 }
这是输出
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
答案 0 :(得分:2)
检查fscanf()
对预期值的返回值,而不是EOF
,这是一个好主意。 @BLUEPIXY @Peter。这将识别/解决OP的大多数问题。
数据文件中包含关键字,也需要进行扫描。 @user3386109
建议使用一个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;
}
输出
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