C语言文件操作(1)
2013-01-19 21:55
169 查看
1,c语言文本视图分为两种,文本视图和二进制视图。两者的区别在于文本视图进行文件读取时c语言会对一些本地环境表示转换成c视图,比如尾行表示\r\n,在用文本视图阅读时看到的是\n,而是用二进制视图阅读时看到的是\r\n。
2,打开文件。fopen().
FILE *fp=fopen( "c:\\test.txt","r" );其中第一个参数是文件名,第二个参数是打开模式。
当打开失败是返回空指针NULL,在打开文件后需要进行判断。
if(fp==NULL)
{
printf("open the file error!\n");
return;
}
文件打开模式主要由r,w,a,t,b,+六个字符拼成:
r(read): 读
w(write): 写
a(append): 追加
t(text): 文本文件,可省略不写
b(banary): 二进制文件
+: 读和写
3,文件关闭fclose().
fclose(fp); 当关闭成功时返回0,失败是返回EOF。
一般关闭文件时这样使用会比较好
if(fclose(fp)!=0)
{
printf("close the file error!\n");
}
2,打开文件。fopen().
FILE *fp=fopen( "c:\\test.txt","r" );其中第一个参数是文件名,第二个参数是打开模式。
当打开失败是返回空指针NULL,在打开文件后需要进行判断。
if(fp==NULL)
{
printf("open the file error!\n");
return;
}
文件打开模式主要由r,w,a,t,b,+六个字符拼成:
r(read): 读
w(write): 写
a(append): 追加
t(text): 文本文件,可省略不写
b(banary): 二进制文件
+: 读和写
3,文件关闭fclose().
fclose(fp); 当关闭成功时返回0,失败是返回EOF。
一般关闭文件时这样使用会比较好
if(fclose(fp)!=0)
{
printf("close the file error!\n");
}