#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
fp=fopen("E:\\Recent Files\\test.txt","r");
//若这里是w模式,那么 下面的代码没有进行写操作,源文件就会变成空的了
//你输出也就没有用了,所以这里我们要注意
if (!fp)
{
printf ("can't find the files\n.");
exit(1);//返回操作系统,关闭所有打开的文件
system("pause");
}
char ch;
while ((ch=fgetc(fp))!=EOF)
fputc(ch,stdout);
//while ((ch=fgetc(stdin))!=EOF)
// fputc(ch,stdout);
fclose(fp);
system("pause");
return 0;
}
正如 我们注释的地方,这个模式 我们一定要 记住不要写错了
#include <stdio.h>
#include <iostream>
using namespace std;
#include <stdlib.h>
int main()
{
FILE *fp1,*fp2;
char buffer[105];
fp1=fopen("E:\\Recent Files\\test.txt","w");
//fp2=fopen("E:\\Recent Files\\cherry.txt","w");
cout<<"11111111fp1="<<fp1<<endl;
if (NULL==fp1)
{
printf ("test cannt open\n");
exit(1);
}
char ch;
while ((ch=fgetc(stdin))!='\n')
{
fputc(ch,fp1);
}
//fclose(fp1);
cout<<"222222fp1="<<fp1<<endl;
system("pause");
return 0;
}
这里我们没有fclose(); 所以在写这个文件时 不能成功
这里 原因我也不清楚 具体的可以看源代码吧
那这个文件会一直被打开知道程序退出,会有一个文件描述符被一直占用直到程序退出,如果一直fopen而不fclose则会导致描述符泄漏。
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1,*fp2;
char buffer[105];
fp1=fopen("E:\\Recent Files\\test.txt","w");
fp2=fopen("E:\\Recent Files\\cherry.txt","w");
if (NULL==fp1)
{
printf ("test cannt open\n");
exit(1);
}
if (NULL==fp2)
{
printf ("cherry cannt open\n");
exit(1);
}
char ch;
while ((ch=fgetc(stdin))!='\n')
{
fputc(ch,fp1);
}
fclose(fp1);
fp1=fopen("E:\\Recent Files\\test.txt","r");
while ((ch=fgetc(fp1))!=EOF)//while (!feof(fp1))
{
fputc(ch,fp2);
}
fclose(fp2);
fp2=fopen("E:\\Recent Files\\cherry.txt","r");
while ((ch=fgetc(fp2))!=EOF)
{
fputc(ch,stdout);
}
fclose(fp1);
fclose(fp2);
system("pause");
return 0;
}
每一次操作完 就要进行关闭文件 估计是指针的问题
这个暂时 还没有搞清楚