2016-12-14 78 views
0

我试图将.txt中的输入文件转换为.csv。我已经使用gdb执行了多个测试并切换了我的代码。代码看起来应该可以工作,但由于某种原因,它没有。我试过使用“while (fscanf(…arguments…) != EOF)”,但当我知道输入文件结束时,我总是以无休止的循环结束。这是我试图读取文件的方式是问题还是别的?我非常感谢任何建议。使用fscanf读取输入文件的问题

输入文件的样本(它太大了。另外,电位值是一直保持为0的唯一价值。所有其他值都大于零)

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 
+2

fscanf'的'检查返回值。如果存在字符串,则读取数值将失败。 – BLUEPIXY

+1

将来,请不要在代码上放行号;这使得很难进行复制和测试。请检查数据的布局;虚线两侧是否有空白线?数据中真的有虚线吗?值得在代码中使用更多的空格,尤其是在逗号之后。 –

+2

第一行包含一个字符串('time:'),一个数字('40')和另一个字符串('ms')。所以第一个'fscanf'需要使用一个格式字符串,比如''%* s%lf%* s“'。注意'fscanf'不会跳过字符串,除非你告诉它。 – user3386109

回答

2

检查日e从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 
+0

用''%* [ - ]“替换'”---------------------------“'是一个好主意'''以允许数据文件中破折号的可能错误? –

+0

@DavidBowling可以采取任何方式。当然,输入文本文件是从代码输出创建的。任何变化都值得调查。 – chux