學習linux編程的一些代碼

來源:互聯網
上載者:User
/****使用execve啟動一個進程,*int execve(const char* path,char *const argv[],...,char *const envp[]);函數原型***/#include <unistd.h>#include <stdio.h>void create_progress(){char *args[]={"/usr/bin/vim",NULL};printf("系統分配的進程號(pid)是:%d\n",getpid()); //調用getpid()顯示輸出進程號if(execve("/usr/bin/vim",args,NULL)<0) /*調用vim程式,建立新的進程*/perror("用execve建立進程出錯!")}/* * 帶緩衝的流檔案 */#include <stdio.h>#include <stdlib.h>void open_file(){    FILE *fp ;          //定義檔案指標    if((fp=fopen("/home/silence/4-9file_tmp","w"))==NULL)          //開啟檔案    {        printf("(建立)檔案出錯"); //出錯處理        exit(0);    }    else {        printf("檔案開啟成功!");    }    system("ls -l /home/silence/4-9file_tmp");    fclose(fp);    }void write_file(){    char ch[20];    FILE *fp;   //定義檔案指標    if((fp=fopen("/home/silence/4-10file","w"))!=NULL) //開啟(建立)檔案    {        printf("檔案開啟成功!\n");    }    else{        printf("檔案開啟失敗!\n");    }    printf("請輸入要寫入的字元!\n");   fgets(ch,20,stdin);   fputs(ch,fp); //把鍵盤輸入的一個字元寫入檔案中        printf("aaaaaaaaaaaa%s",ch);       fclose(fp);           }void read_file(){    char ch[20];    FILE *fp;    if((fp=fopen("/home/silence/4-10file","r"))!=NULL)    {        printf("開啟檔案成功!\n");    }     fgets(ch,20,fp);    printf("bbbbbbb %s\n",ch);    fclose(fp);    }

/*

*對檔案進行塊讀寫。

*/

struct test{char name[20];int pay;}s[nmemb];void fwrite_file(){FILE *fp;set_s(0,"張三",12345);set_s(1,"李四",200);set_s(2,"王五",50000);if((fp=fopen("/home/silence/_file","a+"))==NULL){printf("fwrite_file() 檔案開啟檔案成功!\n");}else{fwrite(s,sizeof(struct test),nmemb,fp); //調用fwrite 函數把塊資訊寫入檔案fclose(fp);}}void fread_file(){FILE *fp;if((fp=fopen("/home/silence/_file","r"))==NULL){printf("fread_file() 開啟檔案成功!\n");}else{fread(s,sizeof(struct test),nmemb,fp); //調用fread函數從檔案讀取塊資訊fclose(fp);int i=0;for(i;i<nmemb;i++){printf("帳號[%d]:%-20s 餘額[%d] : %d\n", i , s[i].name , i , s[i].pay);}}}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.