2014-10-06 76 views
0

比方說,我有文件,其中包含數據格式符號化「...」串

Data1 "X1 Y1 Z1" 
Data2 "X2 Y2 Z2" 
Data3 "X3 Y3 Z3" 

一般情況下,我怎麼會掃描該文件,並讓我的計劃數‘X1 Y1 Z1’作爲一個單一的令牌?

+0

你想讀取文件中的每一行到char數組? – Scooter 2014-10-06 02:45:05

+1

找到第一個'''的位置,第二個'''的位置,然後複製它們之間的所有字符。 – 2014-10-06 02:53:57

+0

你的文件是有data1也寫在它或只有字符串x1 y1 z1,所以存在? – 2014-10-06 06:34:08

回答

2
#include <stdio.h> 

int main(){ 
    FILE *fp = fopen("data.txt", "r"); 
    char data_name[16]; 
    char data_string[32]; 

    while(2==fscanf(fp, "%15s \"%31[^\"]\"", data_name, data_string)){ 
     printf("%s, %s\n", data_name, data_string); 
    } 
    fclose(fp); 
    return 0; 
} 
+0

如果你想讓Data1和「X1 Y1 Z1」進入單個數組,你將如何格式化它? – 2014-10-06 03:20:49

+0

@MatthewAllen例如轉換爲'struct data array [MAX_NUMOFREC];' – BLUEPIXY 2014-10-06 07:27:18