Embedded C language development file IO programming open/close/read/write/stat/fstat/lstat

open/close

   int fd2;
   fd2=open(argv[1],O_RDWR | O_CREAT | O_EXCL,0655);
   if(fd2==-1)
   {
       perror("open file error!");
       exit(1);
   }
   printf("fd2=%d\n",fd2);
  int ret=close(fd2);
   printf("fd2=%d\n",fd2);
   printf("ret=%d\n",ret);

Partial code

O_RDONLY O_WRONLY O_RDWR: read only write only
O_CREAT: create when the file does not exist
O_APPEND: when opening a file, move the file read / write position to the end = = fopen(argv[1], "a +")
O_EXCEL: judge whether the file already exists. If it exists, an error will be reported
O_TRUNC: open the file and clear the file
O_NONBLOCK: open in non blocking mode

read/write

 char buffer[1024];
  // fgets(stdin,buffer,sizeof(buffer-1));
   gets(buffer);
   int num=5;
   int nw=write(fd2,buffer,strlen(buffer));
if(nw==-1)
{
    perror("write error!");
    exit(1);
}
printf("weite data len=%d\n",nw);

int file_size=lseek(fd2,0,SEEK_SET);
printf("file-size=%d\n",file_size);

memset(buffer,0,sizeof(buffer));

int nr=read(fd2,buffer,sizeof(buffer));
buffer[nr]='\0';
printf("read data len =%d data = %s\n",nr,buffer);



   int ret=close(fd2);
   printf("fd2=%d\n",fd2);
   printf("ret=%d\n",ret);

The difference between strlen and sizeof

Differences: 1. sizeof will count the null character 0, while strlen will not count the null character 0; 2. sizeof calculates the last null character 0 of the string and ends. If strlen encounters the first null character 0, it will stop and calculate the length before the first null character 0.

**sizeof() * * is an operator whose value has been calculated during compilation. Parameters can be arrays, pointers, types, objects, functions, etc.
Its function is to obtain the byte size guaranteed to accommodate the maximum object established by the implementation.
Because it is calculated at compile time, sizeof cannot be used to return the size of dynamically allocated memory space. In fact, sizeof is used to return the type and the space occupied by the statically allocated object, structure or array. The return value has nothing to do with the content stored in the object, structure or array.

**strlen(...) * is a function that can only be calculated at run time. The parameter must be a character pointer (char). When the array name is passed in as a parameter, the array actually degenerates into a pointer.
Its function is to return the length of the string.
The string may be self-defined or random in memory. The actual function of this function is to traverse from the first address representing the string until it meets the terminator '\ 0'. The length returned does not include '\ 0'.

stat/fstat/lstat
Get the length of the file:

int file_sizoe = fseek(fd,0,SEEK_END);

//perhaps

struct stat file1;
//stst(argv[1],&file1);// The file is not open with this
fstat(fd2,&file1);//File opened with this
printf("file_size=%d\n",file1.st_size);

Complete code

#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
int main(int argc,char **argv)
{
    if(argc != 2)
    {
        printf("please input file name!\n");
        exit(1);
    }
    //creat(argv[1],S_IRWXU | S_IRWXG | S_IRWXO);
    int fd1;
    fd1=creat(argv[1],0644);
    if(fd1==-1)
    {
       /*if(errno==EISDIR)
       {
           printf("creat file error:filename is dir!\n");
       }
       else if(errno == EINVAL)//can shu error
       {
         
       }
       printf("creat file error!\n");*/
       perror("creat file error!\n");
       exit(1);
    }
   // printf("creat file error!\n");
//printf("creat file error:%s\n",strerror(errno));
   printf("fd1=%d\n",fd1);




   int fd2;
   fd2=open(argv[1],O_RDWR );
   if(fd2==-1)
   {
       perror("open file error!");
       exit(1);
   }
   printf("fd2=%d\n",fd2);
  //int ret=close(fd2);
   //printf("fd2=%d\n",fd2);
   //printf("ret=%d\n",ret);



   char buffer[1024];
  // fgets(stdin,buffer,sizeof(buffer-1));
   gets(buffer);
   int num=5;
   int nw=write(fd2,buffer,strlen(buffer));
if(nw==-1)
{
    perror("write error!");
    exit(1);
}
printf("weite data len=%d\n",nw);

struct stat file1;
//stst(argv[1],&file1);
fstat(fd2,&file1);
printf("file_size=%d\n",file1.st_size);

int file_size=lseek(fd2,0,SEEK_SET);
printf("file-size=%d\n",file_size);

memset(buffer,0,sizeof(buffer));

int nr=read(fd2,buffer,sizeof(buffer));
buffer[nr]='\0';
printf("read data len =%d data = %s\n",nr,buffer);



   int ret=close(fd2);
   printf("fd2=%d\n",fd2);
   printf("ret=%d\n",ret);


    return 0;
}

Keywords: C Linux Back-end ubantu

Added by jtrost on Thu, 17 Feb 2022 09:29:27 +0200