1.txt
内容:
22.468 1 50
33.411 12 3
77.639 29 3
44.811 20 18
我们将读取文件,并把用空格分开的数据分别存放到临时变量并输出
#include <stdio>
int main() { char filename[] = "1.txt"; FILE *fp; if((fp = fopen(filename,"r")) == NULL) { printf("error!"); return -1; } while (!feof(fp)) { float fData = 0; int iData1 = 0, iData2 = 0; fscanf(fp, "%f", &fData); fscanf(fp, "%d", &iData1); fscanf(fp, "%d", &iData2); printf("%f %d %d\n", fData, iData1, iData2);
//这里可以做其他操作,如把数据保存到数组中以便以后使用 } fclose(fp); return 0; }
|
输出:
22.468000 1 50
33.410999 12 3
77.639000 29 3
44.811001 20 18
Press any key to continue
如果是C++的话,可以用CStdioFile
一行行读入,然后再用sscanf把float int int 数据分别读取出来.
阅读(3364) | 评论(1) | 转发(0) |