C语言文件操作

来源:互联网 发布:数据分析公司招聘 编辑:程序博客网 时间:2024/06/03 18:33

下列代码创建一个a.txt文件,权限777,fp为返回码

#include<stdio.h>#include<fcntl.h>int main(){    int fp;    fp=open("a.txt",O_CREAT,777);    printf("%d\n",fp);    close(fp);return 0;}

fd是指文件描述符
open(filename,flag,power)
flag的值可以为O_CREAT(只有这个需要使用第三个参数,第三个参数可以为英文缩写也可以是八进制数字,在第三个参数后面添加一个逗号后,可以跟下面的参数一起使用S_IRUSR
Permits the file’s owner to read it.
S_IWUSR
Permits the file’s owner to write to it.
S_IRGRP
Permits the file’s group to read it.
S_IWGRP
Permits the file’s group to write to it.)|O_RDONLY|O_WRONLY|O_RDWR|O_APPEND(需要跟前面的可写一起用)|O_EXCL(测试是否存在,确保调用者就是文件创造者)|O_TRUNC(如果存在就清空)|O_DIRECTORY(打开一个目录,如果前面的路径需要是目录)|O_NOATIME(不修改最后更新时间)|O_NOFOLLOW(不对符号链接操作),错误函数会返回-1,其中只读和只写和读写都可以和创建或使用(|)表示如果不存在则创建,只读只写只读写不可以一起用

read(fd,buf,size)返回成功读写的字节数,buf为缓冲区,注意输出buf时会多一些奇怪的字符,因为,buf是字符串,还需要在其末尾添加一个字符串结束符\0

write(fd,buf,size)

perror(string)打印错误信息string,已经文件操作返回值errno对应的信息,还有一个strerror(errno),返回对应errno对于的更多相关信息

lseek(fd,offset(偏移量),whence(开始位置))
☆ 欲将读写位置移到文件开头时:lseek(int fd,0,SEEK_SET)
☆ 欲将读写位置移到文件尾时:lseek(int fd,0,SEEK_END)
☆ 想要取得目前文件位置时:lseek(ind fd,0,SEEK_CUR)

#include<stdio.h>#include<errno.h>#include<fcntl.h>#include<string.h>int main(){    int fp;    char bp[20];    scanf("%s",bp);;    fp=open("aaa",O_WRONLY);    perror("open fail");    lseek(fp,10,0);//其实位置为开头,偏移量为10    write(fp,bp,strlen(bp));    perror("write fail");    close(fp);return 0;}

还有一个文件创建函数creat(),它的函数原型是int creat(const char *pathname,int perms)
close(fd)关闭文件

pread(fd,buf,count,offset)其中offset为偏移量,与read函数不同,该函数执行后也不会改变当前偏移量
pwrite(fd,buf,count,offset) 这两个都是原子操作,不可中断,一次性完成

readv(fd,const struct iovec *iov,count)依次填充count个结构体,成功的话会返回读取的数据字节数,注意,如果返回的值不足,说明结构体还有部分没有填充完
writev(fd,const struct iovec *iov,count) 这两个都是原子操作,不可中断,一次性完成

preadv(fd,const struct iovec *iov,count,offset)
pwritev(fd,const struct iovec *iov,count,offset) 跟上面两个函数差不多,不过多了个偏移量,这个对于某些操作很方便

truncate(filename,length)这个很6,不需要open就能直接对文件操作
ftruncate(fd,length)注意fd都是指文件描述符,filename就是文件路径,两个函数都是截取文件长度,如果文件长度大于length,截除后面多余的,如果小于就添加一系列空字符或者文件空洞

如果需要操作大于2个g的文件,需要在宏定义#define _FILE_OFFSET_BITS 64

创建临时文件
mkstemp(path),path为字符数组,生成一个唯一文件名并打开该文件,要求最后六个字符必须为XXXXXX,然后使用unlink(path)删除这些文件
temfile()也可以创建临时文件,用法未知

./a.out result.dat 2>&1重定向标准输出和标准错误到result.dat

下面这两个程序都是自己乱写的,主要是用来练手用的,把这些函数都实践一下罢了

#include<stdio.h>      2 #include<string.h>      3 #include<stdlib.h>      4 int main(){      5     FILE *fp,*fp1,*fp2,*fp3;      6     char ch,str[10];      7     fp=fopen("aaa","w");      8     if(fp==NULL)      9         printf("there is some erro\n");     10     else     11         while((ch=getchar())!=EOF){     12             fputc(ch,fp);     13         }     14     fp1=fopen("bbb","r");     15     if(fp1==NULL)     16         printf("there is sothing wrong\n");     17     else     18         while((ch=fgetc(fp1))!=EOF)     19             putchar(ch);     20     if(fp1==NULL)     21         printf("no this file for fp1\n");     22     else     23         while((fgets(str,10,fp1))!=NULL){//字符串长10     24             printf("%s\n",str);     25         }     26     fp2=fopen("ccc","a");     27     if(fp2==NULL)     28         printf("no this file for fp2\n");     29     else{     30             scanf("%s",str);     31             fputs(str,fp2);     32         }     33     fp3=fopen("linux.txt","w");     34     fseek(fp3,10,0);//10是偏移量,0是原位置     35     fputs("ooooooo",fp3);//在10位置后面插入字符串     36 return 0;     37 }
#include<errno.h>#include<fcntl.h>#include<string.h>int main(){    int fp,fp1,flags,flags1;    char bp[20],tempname[]="/root/bp/kaliXXXXXX";    mkstemp(tempname);    sleep(5);//如果把程序放到后台运行,才能看到临时文件,因为程序一结束,临时文件自动删除    unlink(tempname);    scanf("%s",bp);;    fp=open("aaa",O_WRONLY);    fp1=open("bbb",O_WRONLY);    flags=fcntl(fp,F_GETFL);    flags1=fcntl(fp1,F_GETFL);//change fp1 to fp ,which can change fp file operation status ,such as add O_APPEND    int accessmode=flags & O_ACCMODE;    flags1 |=O_APPEND;    fcntl(fp1,F_SETFL,flags1);    write(fp1,"thisok",7);    if(accessmode & O_WRONLY)        printf("can write\n");    else        printf("flags fail\n");    perror("open fail");    lseek(fp,10,0);    write(fp,bp,strlen(bp));    perror("write fail");    int fd1,fd2,fd3;    fd1=open("a",O_WRONLY);    fd2=dup(fd1);    write(fd1,"kkk",4);    write(fd2,"lll",4);//会在a文件后面添加lll字符串    close(fp);}

头文件介绍
关于头文件

#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>

最简单的方法就是进行文件操作的时候,把上面的头文件全部写进去
stdio.h所包含的函数:

文件访问
fopen
freopen
fflush
fclose
二进制输入/输出
fread
fwrite
非格式化输入/输出
fgetc/getc
fputc/putc
ungetc
fgets
fputs
格式化输入/输出
scanf/fscanf/sscanf
printf/fprintf/sprintf
perror
文件定位
ftell
fseek
fgetpos
fsetpos
rewind
错误处理
feof
ferror
文件操作
remove
rename
tmpfile

stdlib.h头文件

3.1 1函数名称:calloc 3.2 2函数名称:free 3.3 3函数名称:malloc 3.4 4函数名称: realloc 3.5 5函数名称: rand 3.6 6函数名称: abort 3.7 7函数名称: exit 3.8 8函数名称: getenv 3.9 9函数名称: putenv 3.10 10函数名称: labs 3.11 11函数名称: atof 3.12 12函数名称: atoi 3.13 13函数名称: atol 3.14 14函数名称:ecvt 3.15 15函数名称:fcvt 
0 0
原创粉丝点击