C语言文件操作(一)
来源:百度文库 编辑:神马文学网 时间:2024/10/02 19:03:08
C语言提供了4种标准读写文件的方法,相应的也有4种函数:1.fgetc 和 fputc 读写一个字符2.fgets 和 fputs 读写一个字符串3.fscanf 和 fprintf 格式化读写4.fread 和 fwrite 读写数据块,二进制数据读写 读取文本文件的程序示例://读取文本文件的内容 #include "stdafx.h" int main(int argc, char* argv[])
{
//文件结构指针
FILE *fp;
//只读的方式打开文件
fp=fopen("E:/test.txt","r");
//容错处理
if(fp==NULL)
{
printf("文件不存在!");
return 0;
}
char str[100];
char *tempStr;
do
{
tempStr=fgets(str,100,fp);
if(tempStr!=NULL)
printf("%s\n",str);
}while(!feof(fp));//判断是否文件结尾 //关闭文件指针
fclose(fp); return 0;
}
文件的复制,同时适用于二进制文件和文本文件// FileTest.cpp : Defines the entry point for the console application.
//文件的复制 #include "stdafx.h" int main(int argc, char* argv[])
{
//文件结构指针
FILE *fp;
FILE *cp;
//只读的方式打开文件
fp=fopen("E:/test.txt","r");
cp=fopen("E:/copyTest.txt","w");
//容错处理
if(fp==NULL)
{
printf("文件不存在!");
return 0;
}
char str[100];
//返回的已经读取的文件指针位置
unsigned int temp;
do
{
temp=fread(str,sizeof(char),100,fp);
fwrite(str,sizeof(char),temp,cp);
}while(temp==100);//判断是否文件结尾 //关闭文件指针
fclose(fp);
fclose(cp);
return 0;
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liyebing/archive/2008/11/30/3410716.aspx
{
//文件结构指针
FILE *fp;
//只读的方式打开文件
fp=fopen("E:/test.txt","r");
//容错处理
if(fp==NULL)
{
printf("文件不存在!");
return 0;
}
char str[100];
char *tempStr;
do
{
tempStr=fgets(str,100,fp);
if(tempStr!=NULL)
printf("%s\n",str);
}while(!feof(fp));//判断是否文件结尾 //关闭文件指针
fclose(fp); return 0;
}
文件的复制,同时适用于二进制文件和文本文件// FileTest.cpp : Defines the entry point for the console application.
//文件的复制 #include "stdafx.h" int main(int argc, char* argv[])
{
//文件结构指针
FILE *fp;
FILE *cp;
//只读的方式打开文件
fp=fopen("E:/test.txt","r");
cp=fopen("E:/copyTest.txt","w");
//容错处理
if(fp==NULL)
{
printf("文件不存在!");
return 0;
}
char str[100];
//返回的已经读取的文件指针位置
unsigned int temp;
do
{
temp=fread(str,sizeof(char),100,fp);
fwrite(str,sizeof(char),temp,cp);
}while(temp==100);//判断是否文件结尾 //关闭文件指针
fclose(fp);
fclose(cp);
return 0;
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liyebing/archive/2008/11/30/3410716.aspx
C语言文件操作(一)
C语言帝国: MFC文件操作大全(每日更新5个)
C语言 文件操作 -转载自编程爱好者论坛
Linux下C语言编程--文件的操作
C 的文件操作
c文件操作
C语言文件读写纪要
C语言复习笔记(一)
C语言基础教程(一)3
C语言之C语言的底层操作
C 语言之二 底层操作
C 语言之二 底层操作
C语言一夜速成~!!
C语言一夜速成
C语言基础教程(一)基础篇(6)
C语言编程开发中用好位操作符(转)
C 语言之二 底层操作 - C语言技巧讨论 - jiuzhuaxiong
C语言一夜速成学习方法
C语言一夜速成学习方法
匠人的百宝箱--C语言的底层操作
嵌入式C语言位操作的移植与优化
嵌入式C语言位操作的移植与优化1
在linux下使用c语言操作临时文件
C语言嵌入式系统编程修炼之键盘操作篇