|
发表于 2004-10-30 12:29:35
|
显示全部楼层
回复: 一个c语言的小问题,有兴趣的可以看看
最初由 bbcxiaoke 发表
有如下一段代码
- #include "stdio.h"
- int main()
- {
- FILE *fp;
- fp = fopen("tmpfile", "rb");
-
- char *tmpStr = (char*)malloc(50);
- while( !feof(fp) )
- {
- fscanf(fp, "%s", tmpStr);
- printf("str: %s\n", tmpStr);
- }
- fclose(fp);
- }
复制代码
文件tmpfile的内容只有一行为:
china mother bee
程序的输出为:
str: china
str: mother
str: bee
str: bee
把程序稍稍改变一下
- #include "stdio.h"
- int main()
- {
- FILE *fp;
- fp = fopen("tmpfile", "rb");
-
- while( !feof(fp) )
- {
- char *tmpStr = (char*)malloc(50);
- fscanf(fp, "%s", tmpStr);
- printf("str: %s\n", tmpStr);
- }
- fclose(fp);
- }
复制代码
此时程序的输出为
str: china
str: mother
str: bee
str:
不知道哪位能解释一下?
这两个程序都不好
首先:include系统的头文件最好用<>
其次:申请了空间但是没有释放。 |
|