Learn the code of Linux Programming

Source: Internet
Author: User
/***** Use execve to start a process, * int execve (const char * path, char * const argv [],..., char * const envp []); function prototype ***/# include <unistd. h> # include <stdio. h> void create_progress () {char * ARGs [] = {"/usr/bin/Vim", null}; printf ("the process ID assigned by the system (PID) is: % d \ n ", getpid (); // call getpid () to display the output process ID if (execve ("/usr/bin/Vim ", argS, null) <0)/* call the vim program to create a new process */perror ("An error occurred while creating a process with execve! ")}/** Stream file with cache */# include <stdio. h> # include <stdlib. h> void open_file () {file * FP; // defines the file pointer if (FP = fopen ("/home/Silence/4-9file_tmp", "W ")) = NULL) // open the file {printf ("(create) file error"); // handle error exit (0);} else {printf ("file opened successfully! ");} System (" ls-L/home/Silence/4-9file_tmp "); fclose (FP);} void write_file () {char ch [20]; file * FP; // define the file pointer if (FP = fopen ("/home/Silence/4-10file", "W "))! = NULL) // open (create) the file {printf ("file opened successfully! \ N ");} else {printf (" file opening failed! \ N ");} printf (" Enter the character to write! \ N "); fgets (CH, 20, stdin); fputs (CH, FP); // write one character entered by the keyboard into the file printf (" aaaaaaaaaaaa % s ", ch); fclose (FP);} void read_file () {char ch [20]; file * FP; If (FP = fopen ("/home/Silence/4-10file ", "R "))! = NULL) {printf ("file opened successfully! \ N ") ;}fgets (CH, 20, FP); printf (" bbbbbbb % s \ n ", CH); fclose (FP );}

/*

* Reads and writes files in blocks.

*/

Struct test {char name [20]; int pay;} s [nmemb]; void fwrite_file () {file * FP; set_s (0, "James", 12345 ); set_s (1, "", 200); set_s (2, "", 50000); If (FP = fopen ("/home/Silence/_ file ", "A +") = NULL) {printf ("fwrite_file () file opened successfully! \ N ") ;}else {fwrite (S, sizeof (struct test), nmemb, FP); // call the fwrite function to write block information to the file fclose (FP );}} void fread_file () {file * FP; If (FP = fopen ("/home/Silence/_ file", "R") = NULL) {printf ("fread_file () is successfully opened! \ N ");} else {fread (S, sizeof (struct test), nmemb, FP); // call the fread function to read block information from the file fclose (FP ); int I = 0; for (I; I <nmemb; I ++) {printf ("account [% d]: %-20 s balance [% d]: % d \ n ", I, S [I]. name, I, S [I]. pay );}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.