2014-04-05 72 views
0
typedef char string20[21]; 

struct x{ 
string20 a; 
string20 b; 
string20 c; 
}; 

如何掃描文本文件並將其值存儲在我的結構中?我想不出如何做到這一點,順便說一句,我剛學我一個簡單的方法/ O無法找到互聯網上的任何好的教程,請幫助 文件格式爲:未格式化爲C的文件輸入/輸出

3 
FCODE=random 
FKEY=shit 
FSRC=hi 

怎麼辦我存儲「隨機」在等...我知道我應該使用strcpy ofcourse

+0

看這裏http://stackoverflow.com/questions/13832514/from-file-to-structure-in-c?lq=1 –

+0

是的,'strcpy()'是一個安全的賭注。但是請確保不要覆蓋你的數組。**提示:在這裏發佈你的代碼。 – pmg

回答

2

使用fgets函數爲一條線。

eg: fgets(buf, MAX_LINE_SIZE, my_io); 

使用strchrstrtok找到確切的數據。

eg: ptr = strchr(buf, '='); 

拷貝到您的結構

eg: strcpy(my_structy.ele, ptr); 

PS:不要忘了驗證。參考手冊頁