- 论坛徽章:
- 0
|
首先把字符串转化为数字, 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
复制代码 靠, 貌似判断到达文件结尾有点问题。。。 你自己琢磨琢磨
久了没写C, 搞忘了 |
|