Linux 文件 open,creat,write,read (C)

来源:互联网 发布:sql 有事物的存储过程 编辑:程序博客网 时间:2024/05/24 08:33
#include <stdio.h>#include<fcntl.h>  //file control#include<unistd.h>//unix std ,read ,write,getpid#include<stdlib.h>int main(void){    int createResult;    char * filepath="xx.txt";    int file_fd;    char buffer[1024]="this is content ,I Writed!";    printf("Hello World!\n");    createResult=creat(filepath,O_CREAT);    if(createResult==-1)    {        printf("create failure!\n");    }else{        printf("create success!\n");    }    file_fd=open(filepath,O_WRONLY);    if(file_fd==-1)    {        printf("open failure!\n");        exit(1);    }    int haswritted=write(file_fd,buffer,sizeof(buffer));    printf("write %dstring in file \n",haswritted);    close(file_fd);    ////////    file_fd=open(filepath,O_RDONLY);    read(file_fd,buffer,sizeof(buffer));    printf("%s\n",buffer);    close(file_fd);    return 0;}

0 0
原创粉丝点击