linux文件操作

嵌入式文件io操作代码

#include 



//文件操作函数头文件
#include 
#include 
#include 
#include 
#include 

main()
{
    int fd;
    char *test5="/bin/test5";
    char read_buf[1024];
    ssize_t read_len=1024;
   char buf_write[]="\n ========================start=====================\n\n *****this  content is write function test*********\n\n=====================end========================\n";
   // char buf_write[] = "\n Hello Write Function! \n";
    
    if((fd = open(test5,O_RDWR|O_CREAT,0777))<0)
    {
        printf("\n creat file err \n");
    }else
    {
          printf("\n creat file success \n");
    }
    if((write(fd,buf_write,strlen(buf_write)))<0)
    {
        
        printf("\n write %s file err \n",test5);
        
    }else
    {
         printf("\n write %s file ok \n",test5);
        
    }
    close(fd);
    
    
    
      #if 1
     if((fd = open(test5,O_RDWR|O_CREAT,0777))<0)
    {
        printf("\n open file err \n");
    }else
    {
          printf("\n open file success \n");
    }

    memset(read_buf,0,1024);
    if( (read_len=read(fd,read_buf,1024))<0)
    {
        
        printf("\n read %s file err \n",test5);
        
    }else
    {
         printf("\n read %s file ok \n",test5);
        printf("\nfile len:%d file is :%s \n",read_len,read_buf);
    }
    close(fd);
    
    printf("\n all end444666 \n");
    #else
    if((fd = open(test5,O_RDWR|O_CREAT,0777))<0){
    printf("open %s failed!\n",test5);
    }
    if(read(fd,read_buf,1024)){
    perror("read");
    }
    printf("Files Content is %s \n",read_buf);
    close(fd);
     printf("\n all end8888\n");
    #endif
    

}

你可能感兴趣的:(linux文件操作)