文件操作
1,标准输入/输出
文件,被封装各种设备复杂的存取方法
目录 被windows形象化为文件夹路径
文件格式
.doc .jpg .mp3 .mp4 .mp5
打开
int open(const char *pathname,int acess,unsigned mode);
O_RDONLY 只读不写
O_WRONLY 只写不读
O_RDWR 可读可写
O_CREAT 创建新文件
O_EXCL 如果文件存在返回-1,否则新建
O_APPEND 在文件末尾添加
O_TEXT 以文本形式打开
O_BINARY 以二进制文件打开
文件句柄 文件描述符
int fh=open("d:\\auto.bat",O_RDONLY|O_TEXT);
打开data.bin文件,添加
int fh=open("d:\\data.bin",O_WRONLY|O_APPEND|O_BINARY);
int fh=open("d:\\data.bin",O_WRONLY|O_APPEND|O_CREAT|O_BINARY,0666);
int creat(const char *pathname,void *buf,unsigned len);
2.读文件
int read(int handle,void *buf,unsigned len);
3.写文件
int write(int handle,void *buf,unsigned len);
4.关闭文件
int close(int handle);
5.文件定位
随时让文件指针指向文件的任意位置
long lseek(int handle,long offset,int formwhere);
offset从 formwhere开始偏移 offset个字节
fromwhere的范围:
SEEK_SET 从文件头移动
SEEK_CUR 从文件当前位置移动
SEEK_END 从文件末尾移动
eg:
从当前位置向后移动4个字节
lseek(fh,SEEK_CUR);
跳到文件尾倒数10个字节
int curROS=lseek(fh,10L,SEEK_END);
int filelenth=lseek(fh,10L,SEEK_END);
文件错误处理
void perror(const char *s);
文件高级处理
fopen() fclose()
FILE *fopen(const *filename,const char *mode);
int close(FILE *fp);
文件打开开方式
"r"只读
"w"只写,如果文件存有内容则覆盖
"a"只写,写入位置在文件末尾,不覆盖
"+" 组合以上方式
"b"二进制
文件读写
读字符:
int fgetc(FILE *fp);
fp读一个字符,到了末尾,返回EOF;
int fputc(int c,FILE *fp);
像fp输出字符c,写入错误返回EOF
读写字符串
char *fget(char *s,int n,FILE *fp);
char *puts(char *s,FILE *fp);
文件定位
int fseek(FILE *fp,long offset,int formwhere);
void rewind(FILE *fp);
long ftell(FILE *fp);
判断文件是否结束
int feof(File *fp);
判断前一次文件操作是否有错误发生
int ferror(FILE *fp);
int fflush(FILE *fp);
文件基本操着(写文件):
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#define FILENEAME "chenliang.bin"
int main()
{
int i=100;
float f=100.0;
char string[]="100";
int fh,rth;
fh=open(FILENEAME,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,066);
//open(a,b,c)打开文件
//a:文件的地址
//b:O_RDONLY只读、O_WRONLY只写、
//O_CREAT建立新文档、O_TRUNC如果文件存在删除再打开
// O_BINARY二进制打开文件
//c:文件类型及权限标志,当b包含 O_CREAT时有效,一般为0666
//文件打开失败返回-1,成功则返回(文件句柄)
if(fh==-1)//错误处理
{
printf("打开文件出错1");
perror(FILENEAME);//输出标准错误字符串,并附上错误文字说明
return -1;//返回错误值给系统
}
rth=write(fh,&i,sizeof(i));
//write(a,b,c)
//a:打开文件时,open()函数返回的文件句柄
//b:写入的值的首地址地址
//c: 写入值的字节长度
//返回值:实际写入的字节数;-1代表出错
if(rth<0)//出错处理
{
printf("i值输入出错2");
perror(FILENEAME);
return -1;
}
//stm32f030c6tx
rth = write(fh,&f,sizeof(f));
if(rth<0)
{
printf("f值输入出错3");
perror(FILENEAME);
return -1;
}
rth =write(fh,"END",strlen("END")+1);
if(rth<0)
{
printf("END输入出错4");
perror(FILENEAME);
return -1;
}
close(fh);
return 0;
}
文件基本操着(写文件):
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#define FILENEAME "chenliang.bin"
int main()
{
int i,j;
float f;
char string[32];
int fh,rth;
fh=open(FILENEAME,O_RDONLY|O_BINARY);
//O_RDONLY只读、O_BINARY二进值打开
if(fh==-1)
{
printf("打开文件出错1");
perror(FILENEAME);
return -1;
}
rth=read(fh,&i,sizeof(i));
//read(a,b,c)
//a:打开文件时,open()函数返回的文件句柄
//b:读入的值的首地址地址
//c: 读入值的字节数
//返回值:实际写入的字节数;-1代表出错
if(rth<0)
{
printf("i值读取出错2");
perror(FILENEAME);
return -1;
}
printf("The int is %d\n",i);
rth = read(fh,&f,sizeof(f));
if(rth<0)
{
printf("f值读取出错2");
perror(FILENEAME);
return -1;
}
printf("The string is%f\n",f);
rth = read(fh,string,sizeof(string));
for(j=0;j<rth;j++)
{
putchar(string[j]);
}
close(fh);//关闭文件
return 0;
}
文件高级操作(写文件):
#include <stdio.h>
int fun(int x)
{
int k;
for(k=2;k<=x/2;k++)
if(x%k==0) return 0;
return 1;
}
int main()
{
int i,n=0;
FILE *px;//文件指针
px=fopen("d:\\x.txt","w");//打开文件,写
if(px==NULL)//文件打开失败
{
printf("open file failed!\n");
return 1;
}
for(i=2;i<100;i++)
if(fun(i))//判断素数
{
printf("%5d",i);
fprintf(px," %4d",i);
n++;
}
fclose(px);
return 0;
}
文件高级操作(读文件):
#include <stdio.h>
int main()
{
int i,sum=0;
FILE *px;
px=fopen("d:\\x.txt","r");
while(!feof(px))
{
fscanf(px,"%d",&i);
sum=sum+i;
}
printf("\nsum=%d\n",sum);
fclose (px);
return 0;
}