linux平台C语言打开文件

来源:互联网 发布:php header跳转404 编辑:程序博客网 时间:2024/05/16 15:01
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int main(int argc ,char *argv[]){    int fd;    if(argc<2){        puts("please input the open file pathname!\n");        exit(1);    }        //如果flag参数里有O_CREAT表示,该文件如果不存在,系统则会创建该文件,该文件的权限由第三个参数决定,此处为0755    //如果flah参数里没有O_CREAT参数,则第三个参数不起作用.此时,如果要打开的文件不存在,则会报错.    //所以fd=open(argv[1],O_RDWR),仅仅只是打开指定文件    if((fd=open(argv[1],O_CREAT|O_RDWR,0755))<0){        perror("open file failure!\n");        exit(1);    }else{        printf("open file %d  success!\n",fd);    }    close(fd);    exit(0);    }

跨平台打开文件

#include <stdio.h>#define LENGTH 100main(){ FILE *fd; char str[LENGTH]; fd = fopen("hello.txt", "w+"); /* 创建并打开文件 */ if (fd) {  fputs("Hello, Software Weekly", fd); /* 写入Hello, software weekly字符串 */  fclose(fd); } fd = fopen("hello.txt", "r"); fgets(str, LENGTH, fd); /* 读取文件内容 */ printf("%s\n", str); fclose(fd);} 


0 0