我有被格式化這樣的約2000文本至極的行的文本文件:無法提取文本文件中的值用C
1 1名1 LastN1 58℃1600 1310.40 6 1 0.22 2164.80 1
2 1 Name2 LastN2 22 d 1700 1523.37 4 1 0.13 897.26 1
3 1 Name3 LastN3 34 c 1600 1195.84 2 1 0.26 836.16 1
。
。
2000年第3 NameX LastNX 46 d 6000 6000.00 1 0 0.00 0.00 1
我想要做的就是從文本文件閱讀所有這些值,並將其儲存到陣列至極什麼是:
int id [2100];
char nombre [2100][30];
char apellido [2100][30];
int edad [2100];
int puesto [2100]; char categoria [2100];
int sueldoI [2100];
float sueldoA [2100];
int antiguedad [2100];
int inscrito [2100];
float aporte [2100];
float ahorro [2100];
int libre [2100];
但是,當我試圖閱讀,我收到了很多垃圾到控制檯
這是我使用的嘗試讀取並存儲到數組方式:
//Way number 1
char linea[70];
while(fgets(linea,70,datos) != NULL){
flushall();
sscanf(linea,"%d %d %s %s %d %c %d %f %d %d %f %f %d\n",&id[i],&puesto[i],&nombre[i],&apellido[i],&edad[i],&categoria[i],&sueldoI[i],&sueldoA[i],&antiguedad[i],&inscrito[i],&aporte[i],&ahorro[i],&libre[i]);
i++;
}
// Way number 2 in here i get linea the way it's intended to be but i can't figure
// out a way to split the string into the multiple values i need to store in the arrays
while(fgets(linea,70,datos) != NULL){
printf("%s",linea);
}
這是輸出我在第一種方式獲得:
編輯:
我已經修改了數組的大小二一〇〇年至2000年,程序似乎表現更好
你能再發表一些代碼嗎?具體來說,你有什麼聲明'linea'爲? – 2012-07-12 04:54:02
當然我會將它添加到問題,但linea聲明爲'char linea [70];' – 2012-07-12 04:54:53
第一種方式的輸出是什麼? – 2012-07-12 04:58:53