the C system invoke operation of Linux, Now let's practice the simple system invoke operation .
Read system call Test
/*************************************************************************> File name:read.c> Author: > Mail: > Created time:tue 01:23:58 AM PST **************************************************************** /#include <stdio.h> #include <unistd.h> #include <errno.h>//using standard input//echo hello for piping test procedures World |./read//./read < a.txt int main () { char buf[128]; int Len; Reads data from the input stream into buffer if return-1 then indicates a read error if ((Len=read (0,buf,128)) ==-1) { //error write standard error output write (2, "read Error\n ", one); } Outputs data read through standard input to console if (write (1,buf,len)!=len) { //If the length of the output is not equal to Len description IO error write (2, "read Error\n ", one); } Exit (0); return 0;}
Write system call Test
/*************************************************************************> File name:write.c> Author: > Mail: > Created time:tue 12:39:05 AM PST **************************************************************** /#include <stdio.h>//linux and Uninx the last error with the encapsulation #include<unistd.h>//errno system called System #include< errno.h>//string output by automatically opening a file handle 1 that is standard output int main () { if (write (1, "hello,world\n", 2)!=12) { Write (2 , "Write error!\n", (); } printf ("errorno:%d\n", errno); Exit (0);}
Open System call Test 1
/*************************************************************************> File name:open.c> Author: > Mail: > Created time:tue 02:07:54 AM PST **************************************************************** /#include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h># Include <stdlib.h> int main () {char*filein= "./in.txt"; Char*fileout= "./out.txt"; int fdin,fdout; Char Rdchar; Open the input file as read-only if ((Fdin=open (filein,o_rdonly)) ==-1) {Write (2, "openerror!\n", 11); return 0; }//Open file file does not exist and create file file exists will empty file and re-write permission to file owner if ((Fdout=open (fileout,o_creat| o_wronly| o_trunc,s_irusr| S_IWUSR)) (==-1) {//error write (2, "openerror!\n", 11); return 0; }//File copy via system call while (read (fdin,&rdchar,1) ==1) {write (fdout,&rdchar,1); }exit (0) return 0;}
Open System call Test 2
#include <stdio.h> #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include < Sys/types.h>int Main () { int fdin,fdout,len; Char buf[10]; Fdin=open ("./in.txt", o_rdonly); Fdout=open ("./out.out", o_wronly| o_trunc| o_creat,s_irusr| S_IWUSR); The high efficiency of the writing system calls as few calls while ((Len=read (fdin,buf,10)) >0) { write (Fdout,buf,len); } return 0;
lseek system call Test
/*************************************************************************> File name:lseek.c> Author: > Mail: > Created time:tue 05:02:51 AM PST **************************************************************** /#include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <fcntl.h># include<sys/stat.h> #define Std_input 0#define std_output 1#define std_error 2//function reads the second and second-to-last characters of the user output the input string is greater than 5int Main () {char input[256]; int Rdlen,wlen; int fdwrite,fdread; Char Rdchar; Rdlen=read (std_input,input,256); if (rdlen==-1) {write (Std_error, "error\n", 6); Exit (0); } if ((Fdwrite=open ("./res.txt", o_creat| o_trunc| o_wronly,s_irusr| S_IWUSR)) (==-1) {write (Std_error, "Create File ERROR", 17); Exit (0); } wlen=write (Fdwrite,input,rdlen); if (wlen==-1) exit (0); Close (Fdwrite); if ((Fdread=open ("./res.txt", o_rdonly)) ==-1) exit (0); Lseek (Fdread,1,seek_set); Read (fdread,&rdchar,1); printf ("index2:%c\n", Rdchar); Lseek (Fdread,-3,seek_end); Read (fdread,&rdchar,1); printf ("index-2:%c\n", Rdchar); Close (Fdread); return 0;}
Linux C Programming-----file operation (1) Simple operation via system call standard input, standard output, standard error