- 論壇徽章:
- 0
|
首先把字符串轉(zhuǎn)化為數(shù)字, C 語(yǔ)言已經(jīng)有現(xiàn)成的函數(shù)可以處理。所以不必糾結(jié)。
測(cè)試文件: number.txt
C程序源文件: test.c
編譯器: gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11)- [root@localhost ~]# cat number.txt
- 18
- 903
- 234
- 45
- 23
- [root@localhost ~]# cat test.c
- #include <stdio.h>
- #include <stdlib.h>
- #define LEN 1024
- int main(void) {
- char str[LEN];
- int num = 0;
- FILE *fp;
- fp = fopen("number.txt","r");
- while (!feof(fp)) {
- fgets(str, LEN, fp);
- num = atoi(str);
- printf("number is: %d\n", num);
- }
- fclose(fp);
- return 0;
- }
- [root@localhost ~]# ./a.out
- number is: 18
- number is: 903
- number is: 234
- number is: 45
- number is: 23
- number is: 23
復(fù)制代碼 靠, 貌似判斷到達(dá)文件結(jié)尾有點(diǎn)問(wèn)題。。。 你自己琢磨琢磨
久了沒(méi)寫(xiě)C, 搞忘了 |
|