关于网友提出的“ C语言中Linux下打开文件函数 freopen问题”问题疑问,本网通过在网上对“ C语言中Linux下打开文件函数 freopen问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: C语言中Linux下打开文件函数 freopen问题描述:
#include
#include
#include
main()
{
FILE *fp;
char path[]="/root/txt1.txt";
extern int errno;
fp=freopen("/root/txt1.txt","r",fp);
if(fp==NULL)
{
printf("cant't open file %s.\n",path);
printf("errno:%d\n",errno);
printf("ERR :%s\n",strerror(errno));
return;
}
else
{
printf("%s was opened.\n",path);
}
}
运行环境为ubuntu11.04 。 出现段错误。不知道问题在哪里?
解决方案1:
...是打开一个fp,让它成为freopen的第二个参数。
解决方案2: fp没打开文件,怎么reopen啊?
freopen的原理就是将一个FILE*里的fd使用dup2重定向到一个新的文件,在这里叫做:"/root/txt1.txt"。
你fp都没打开文件,重定向谁?