我試圖將.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
fscanf'的'檢查返回值。如果存在字符串,則讀取數值將失敗。 – BLUEPIXY
將來,請不要在代碼上放行號;這使得很難進行復制和測試。請檢查數據的佈局;虛線兩側是否有空白線?數據中真的有虛線嗎?值得在代碼中使用更多的空格,尤其是在逗號之後。 –
第一行包含一個字符串('time:'),一個數字('40')和另一個字符串('ms')。所以第一個'fscanf'需要使用一個格式字符串,比如''%* s%lf%* s「'。注意'fscanf'不會跳過字符串,除非你告訴它。 – user3386109