关于读写文件的操作
2013-11-08 12:07
155 查看
program1.写文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE *fp;
char ch;
if((fp=fopen("test2.txt","wt"))==NULL)
{
printf("Cannot open file!\n");
getch();
exit(1);
}
printf("Please input a character:");
//ch=getchar();
/* while(ch!='\n')
{
fputc(ch,fp);
putchar(ch);
ch=getchar();
}*/
while((ch=getchar())!='\n')
{
fputc(ch,fp);
}
//以下两个fputc()用于连接文件时换行
//fputc( '\r', fp );
// fputc( '\n', fp );
fclose(fp);
system("pause");
return 0;
}
program2.读文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE *fp;
char ch;
if((fp=fopen("test2.txt","rt"))==NULL)
{
printf("Cannot open file!\n");
getch();
exit(1);
}
printf("Please input a character:\n");
//ch=fgetc(fp);
while((ch=fgetc(fp))!=EOF)
{
putchar(ch);
}
fclose(fp);
printf("\n");
system("pause");
return 0;
}
program3.读写合并文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE *fp,*fp1,*fp2;
char ch;
if((fp=fopen("test3.txt","wt"))==NULL)
{
printf("Cannot open file!\n");
getch();
exit(1);
}
printf("The file test.txt is open!\n");
if((fp1=fopen("test1.txt","rt"))==NULL)
{
printf("Cannot open file!\n");
getch();
exit(1);
}
printf("The file test1.txt is open!\n");
if((fp2=fopen("test2.txt","rt"))==NULL)
{
printf("Cannot open file!\n");
getch();
exit(1);
}
printf("The file test2.txt is open!\n");
while((ch=fgetc(fp1))!=EOF)
{
fputc(ch,fp);
}
fputc( '\n', fp );
while((ch=fgetc(fp2))!=EOF)
{
fputc(ch,fp);
}
/*
fclose(fp1);
fclose(fp2);
fclose(fp);
*/
_fcloseall(); //fcloseall(); 用不了
printf("文件合并完成!\n");
system("pause");
return 0;
}
program1运行结果:
test2.txt
ABCDEFGHJ
若有
test1.txt
abcefghjk
则program3运行结果:
test3.txt
ABCDEFGHJ
abcefghjk
如果再program3程序的两个读操作之间不加语句 fputc( '\n', fp ); ,则 program3运行结果为
test3.txt
ABCDEFGHJabcefghjk
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE *fp;
char ch;
if((fp=fopen("test2.txt","wt"))==NULL)
{
printf("Cannot open file!\n");
getch();
exit(1);
}
printf("Please input a character:");
//ch=getchar();
/* while(ch!='\n')
{
fputc(ch,fp);
putchar(ch);
ch=getchar();
}*/
while((ch=getchar())!='\n')
{
fputc(ch,fp);
}
//以下两个fputc()用于连接文件时换行
//fputc( '\r', fp );
// fputc( '\n', fp );
fclose(fp);
system("pause");
return 0;
}
program2.读文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE *fp;
char ch;
if((fp=fopen("test2.txt","rt"))==NULL)
{
printf("Cannot open file!\n");
getch();
exit(1);
}
printf("Please input a character:\n");
//ch=fgetc(fp);
while((ch=fgetc(fp))!=EOF)
{
putchar(ch);
}
fclose(fp);
printf("\n");
system("pause");
return 0;
}
program3.读写合并文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE *fp,*fp1,*fp2;
char ch;
if((fp=fopen("test3.txt","wt"))==NULL)
{
printf("Cannot open file!\n");
getch();
exit(1);
}
printf("The file test.txt is open!\n");
if((fp1=fopen("test1.txt","rt"))==NULL)
{
printf("Cannot open file!\n");
getch();
exit(1);
}
printf("The file test1.txt is open!\n");
if((fp2=fopen("test2.txt","rt"))==NULL)
{
printf("Cannot open file!\n");
getch();
exit(1);
}
printf("The file test2.txt is open!\n");
while((ch=fgetc(fp1))!=EOF)
{
fputc(ch,fp);
}
fputc( '\n', fp );
while((ch=fgetc(fp2))!=EOF)
{
fputc(ch,fp);
}
/*
fclose(fp1);
fclose(fp2);
fclose(fp);
*/
_fcloseall(); //fcloseall(); 用不了
printf("文件合并完成!\n");
system("pause");
return 0;
}
program1运行结果:
test2.txt
ABCDEFGHJ
若有
test1.txt
abcefghjk
则program3运行结果:
test3.txt
ABCDEFGHJ
abcefghjk
如果再program3程序的两个读操作之间不加语句 fputc( '\n', fp ); ,则 program3运行结果为
test3.txt
ABCDEFGHJabcefghjk
相关文章推荐
- 关于文件读写操作中ios::app与ios::ate的区别
- C#中关于文件读写操作和注册表操作
- java关于文件读写操作的复习
- 关于java下的明确编码方式的文件读写操作总结
- 关于java中的文件读写操作
- 【工作总结】一组关于key=value类似配置文件的读写操作函数
- 【转载】关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 高级操作
- c++关于文件的读写操作
- [IOS]关于文件(文件目录)的读写操作
- 关于对同一个txt文件的读写操作Java
- Android 中关于文件的读写操作
- 关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 高级操作
- Java基础———重温关于的文件读写操作,File类,FileInputStream类等的使用
- C/C++关于文件的读写操作以及文件的打开和保存
- 关于文件的读写操作以及文件的打开和保存
- 关于在Oracle中读写文件的操作
- 关于文件的读写操作以及文件的打开和保存
- 关于文件的读写操作
- DELPHI关于文件的操作
- 关于c#的config配置文件操作网上很多讲的和实际运用感觉不符,终于理解了一点点,记录一下