There are some difficulties in watching Vedon video Linux driver, the reason, although the application of Linux programming, but did not understand, the correlation function usage is not clear, just see the country embedded video on this aspect is more thorough,
So the learning process is recorded, but also as a Linux application development of a series it!
There are two ways of file programming, one is the system call method, and the other is the library function call.
The former relies on a specific platform, which does not rely on the platform.
System invocation: Creating
int creat (const char *filename,mode_t mode);
FileName: The file name to create
Mode: Create pattern
S_irusr->1
S_iwusr->2
S_iwxusr->4
S_irwxu->7
Examples of system calls:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
void Create_file (char *filename)
{
if (creat (fileanme.,0755) <0)
{
printf ("Create file%s is failuer!\n", filename);
}
Else
{
printf ("Create file%s is success!\n", filename)
}
}
int main (int argc,char *argv[])
{
int i;
if (argc<2)
{
Perror ("You haven ' t-input the filename, please try agin!\n");
Exit (Exit_failuer);
}
for (i=1;i<argc;i++)
{
Create_file (Argv[i]);
}
Exit (exit_success);
}
File Description: File descriptor range 0-open-max. Early allow each process to open 20. Now some of them have grown to 1024.
System Call-Open
int open (const char*pathname,int flags);
int open (const cahr*pathname,int falgs,mode_t mode);
Flags: Open Flag
O_rdonly
O_wronly
O_rdwr
O_APPEBD: Append mode Open
O_creat: the function int open (const cahr*pathname,int falgs,mode_t mode) must be used;
O_noblock: Open non-blocking mode
Example:
#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 yhe Open file pathname!\n");
Exit (1);
}
if ((Fd=open (argv[1],o_creat|) creat_ordwr,0755) <0)
{
Perror ("Open File failuer!\n");
Exit (1);
}
Else
{
printf ("Open file%d is success!\n", FD);
}
Close (FD);
Exit (0);
}
File Close:
int close (FD);
System call-Read
int read (int fd, const void *buf,size_t length);
Reads a length byte from the file specified by the file descriptor FD to the buffer specified by BUF, returning the actual number of bytes read.
Write: int write (int fd, const void *buf,size_t length);
Positioning:
int lseek (int fd,offset_t offset,int whence);
Moves the file read-write pointer to offset bytes relative to whence. When the operation succeeds, returns the position of the file pointer relative to the file header.
Whence:
Seek_set: Relative file start
Seek_cur: The current position of the relative file read-write pointer
Seek_end: Relative file end
Calculate file Length:
System call-access judgment:
int access (const char*pathname,int mode);
Mode: The access permission to be judged. You can take the following values or their combinations
R_ok
W_ok
X_ok
F_OK: File exists
Successfully returns 0, otherwise the condition does not conform to return 1.
Example:
#include <unistd.h>
int main ()
{
if (Access ("/etc/passwd", R_OK) ==0)
printf ("/etc/passwd can be read!" \ n);
}
Linux application Development-file programming-system call mode