LINUX C语言写一个读写文件的程序,读取的话,要把文件内容显示出来,写的话,就是写进文件里面咯
2个回答
展开全部
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *pf = fopen("a.txt", "r");
if(pf == NULL) {
printf("open a.txt file failed!\n");
exit(0);
}
FILE *pf2 = fopen("b.txt", "w");
if(pf2 == NULL) {
printf("open b.txt file failed!\n");
fclose(pf);
exit(0);
}
char ch;
while(!feof(pf)) {
ch = fgetc(pf);
putchar(ch);
fputc(ch, pf2);
}
fclose(pf2);
fclose(pf);
return 0;
}
展开全部
FILE *fp;
char c1[512];
char c2[512];
int i=0;
if((fp=fopen("/路径",'r'))=NULL)
printf("cant open the file");
//读出文件内容并显示
while(!feof(fp))
{
c1[i++]=fgetc(fp);
}
printf("%s\n",c1);
fclose(fp);
//向文件内写内容
if((fp=fopen("/路径",'w'))=NULL)
printf("cant open the file");
printf("please input c2:\n");
scanf("%s",c2);
fprintf(fp,"%s",c2);
fclose(fp);
char c1[512];
char c2[512];
int i=0;
if((fp=fopen("/路径",'r'))=NULL)
printf("cant open the file");
//读出文件内容并显示
while(!feof(fp))
{
c1[i++]=fgetc(fp);
}
printf("%s\n",c1);
fclose(fp);
//向文件内写内容
if((fp=fopen("/路径",'w'))=NULL)
printf("cant open the file");
printf("please input c2:\n");
scanf("%s",c2);
fprintf(fp,"%s",c2);
fclose(fp);
追问
头文件之类的不用加吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询