文件管理I/O笔记

来源:互联网 发布:java嗖嗖移动业务大厅 编辑:程序博客网 时间:2024/05/18 00:21

1.文件一共有六中类型:

链接文件l,普通文件-,设备文件(字符文件和块文件),管道文件p,套接字文件,目录文件

2.文件的一些函数的基本的使

文件的打开权限,基本上是0666,更具w对应为2,r对应为4,x对应为1

几个函数:

creat("./helo",0666)//

chdir("/tmp");//修改文件的主目录,这样文件的位置就不是在当前位置了,在其它文件下创建可能储蓄哦

perror(fd);//显示上一个出错的函数,本身就带着:

3.写入出错时,很可能就是文件的权限的问题

注意点:如果又想创建,又想文件的读写,则最好用open 函数

open(打开文件)
相关函数
read,write,fcntl,close,link,stat,umask,unlink,fopen
表头文件
#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>
定义函数
int open( const char * pathname, int flags);int open( const char * pathname,int flags, mode_t mode);lseek:

lseek(移动文件的读写位置)
相关函数
dup,open,fseek
表头文件
#include<sys/types.h>#include<unistd.h>
定义函数
off_t lseek(int fildes,off_t offset ,int whence);
函数说明
每一个已打开的文件都有一个读写位置,当打开文件时通常其读写位置是指向文件开头,若是以附加的方式打开文件(如O_APPEND),则读写位置会指向文件尾。当read()或write()时,读写位置会随之增加,lseek()便是用来控制该文件的读写位置。参数fildes 为已打开的文件描述词,参数offset 为根据参数whence来移动读写位置的位移数。
参数
whence为下列其中一种:SEEK_SET 参数offset即为新的读写位置。SEEK_CUR 以目前的读写位置往后增加offset个位移量。SEEK_END 将读写位置指向文件尾后再增加offset个位移量。当whence 值为SEEK_CUR 或SEEK_END时,参数offet允许负值的出现。下列是教特别的使用方式:1) 欲将读写位置移到文件开头时:lseek(int fildes,0,SEEK_SET);2) 欲将读写位置移到文件尾时:lseek(int fildes,0,SEEK_END);3) 想要取得目前文件位置时:lseek(int fildes,0,SEEK_CUR);
返回值
当调用成功时则返回目前的读写位置,也就是距离文件开头多少个字节。若有错误则返回-1,errno 会存放错误代码。
附加说明
Linux系统不允许lseek()对tty装置作用,此项动作会令lseek()返回ESPIPE。

#include<sys/types.h>//是Unix/Linux系统的基本系统数据类型的头文件,含有size_t,time_t,pid_t等类型。#include<sys/stat.h>//获取一些文件相关的信息#include<fcntl.h>//这个是为了使用fcntl.h定义了很多宏和open,fcntl函数原型#include<stdio.h>#include<errno.h>//用strerror(error)int main(){int fd;       //11        umask(0);//填充0表示奖mask 编程0,只使用程序中的这个值,不需要减去系统中的值fd=creat("./helo",0666);if(fd==-1){perror("create error");return -1;}printf("%d",fd);int i;for(i=0;i<10;i++){printf("第%d :%s\n",i,strerror(i));//这时候要用:}int fd;        //22        fd=open("./helo.txt",O_RDONLY|O_CREAT,0666|O_RDWR);//不存在就创建文件if(-1==fd){              //33                 printf("%s",strerror(errno));//这时候要用:printfperror("open error");//根据上一个调用的函数t,后面不要加冒号return -1;}   printf("%d",fd);//这里文件fd为文件描述符,输出到标准输出里面去,不是输出到屏幕上   close(fd);return 0;}//文件读写#include<fcntl.h>#include<stdio.h>#include<errno.h>//用strerror(error)#include<unistd.h>int main(){    int fd;    fd=open("helo",O_RDWR|O_CREAT,0666);    if(fd==-1)    {        perror("create error");        return -1;    }    char buf[10]="hell0";    int a=write(fd,buf,strlen(buf));    if(-1==a)    {        perror("write error");        return -1;    }    printf("%d\n",a);    lseek(fd,0,SEEK_SET);    //    lseek(fd,-12,SEEK_CUR);//当前位置前12个    int b=read(fd,buf,10);//位置胡相后    if(-1==b)    {        perror("read error");        return -1;    }    printf("%d",b);    buf[a]='\0';    printf("buf:%s",buf);    return 0;}
4.内存空间的划分:

堆:从malloc()分配的内存,进程由os分配,没回收会导致进程泄露,free()可以用来回收

栈:局部变量放在栈上。当一个函数返回时,所有的栈空间被收回

常量区:不允许被修改,入“helloworld”,当进程退出时被释放,所以期间时一直存在的

代码区:也不能被修改

全局区:全局初始化区和未初始化区

            int t=1;                  int t;默认为0

静态存储区:和全局区相连 static声明的变量,里面的值也默认为0

#include <stdio.h>#include <stdlib.h>char *func(){    char *s="hello";//    return s;}char *fun2(){    char p[]="为为为为为";//而这个时存在栈空间的,调用结束函数返回时,里面数组所有的空间被释放调,但是地址是还在的,只是里面没树了    return p;}int main(void) {    char *s=NULL;    char *q=NULL;    s=func();//hello的首地址    q=fun2();//结果是乱码或者是出现断错误    printf("%s\n",s);    printf("%s\n",q);    //puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */    return EXIT_SUCCESS;}

检验文件是否可读:


    int ret=access(argv[1],R_OK|F_OK);
    if(ret==-1)
    {
        perror("access");
    }
    else
    {
        printf("[%s] exist and executeable",argv[1]);
    }
    if(fd1==-1)
    {
        perror("source file wrong");
        return -1;
    }