读书人

C小疑点

发布时间: 2012-03-03 15:33:03 作者: rapoo

C小问题
今天为了熟悉公司的开发环境,随便写了个程序,出现了奇怪的问题大家看看.
#include <stdio.h>
int main()
{
FILE *fp;
char ch;
fp = open( "text ", "r ");
if(fp == NULL)
{
printf( "cannot open this file ");
exit(0);
}
ch = fgetc(fp);
while (ch!=EOF)
{
putchar(ch);
ch = fgetc(fp);
}
fclose(fp);
return 0;
}
Warning :assingment makes pointer from integer without a cast
Warning :no newline at end of file.




[解决办法]
fp = open( "text ", "r ");
改为:
fp = fopen( "text ", "r ");

文件最后需要多加一个空行。
[解决办法]
#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *fp;
char ch;
fp = open( "text ", "r "); // 这儿fp = fopen( "text ", "r ")
if(fp == NULL)
{
printf( "cannot open this file ");
exit(0); // 居然用了这个就要在前面加上头文件#include <stdlib.h>
}
ch = fgetc(fp);
while (ch!=EOF)
{
putchar(ch);
ch = fgetc(fp);
}
fclose(fp);
return 0;
}
Warning :assingment makes pointer from integer without a cast
Warning :no newline at end of file.
[解决办法]
fp = open( "text ", "r "); //fopen()
第二个经过你再文件的末尾添个回车就是了.不过不影响使用.
[解决办法]
我运行了一下,可以了,不会有警告,最好事先在当前目录下存放一个text.txt文件,而且里面最好有信息。
不知道楼主所说的两个警告分别出现在什么位置,楼主可以根据警告发生的位置去理解和修改

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *fp;
char ch;
fp = fopen( "text.txt ", "r ");

if(fp == NULL)
{
printf( "cannot open this file ");
exit(0);
}
ch = fgetc(fp);

while (ch!=EOF)
{
putchar(ch);
ch = fgetc(fp);
}

fclose(fp);

return 0;
}


读书人网 >C语言

热点推荐