溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

[Linux文件]將用戶輸入的字符串寫入文件實(shí)例

發(fā)布時(shí)間:2020-06-04 16:25:35 來源:網(wǎng)絡(luò) 閱讀:926 作者:銀河星君 欄目:編程語言
 //使用gets函數(shù)從標(biāo)準(zhǔn)輸入(鍵盤)獲得一個(gè)以回車換行為結(jié)束的字符串,可以帶空格
 //運(yùn)行時(shí)候屏幕會提示輸入字符處,以回車結(jié)尾
 //需要注意的是待輸入的字符串存放在writebuf中,不能超過30字節(jié)并且不會帶回車換行
 #include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
 int main(int argc,char *argv[])
 {
   int fd;      //文件描述符
   int temp;    //臨時(shí)變量
   char writebuf[30];  //用于存放寫入字符串
   if(argc != 2)     //如果參考字符串錯(cuò)誤
   {
     printf("Plz input the correct file name as 'exam307WriteFun filename'\n");
     //輸出提示字符串
     return 1;
   }
   else
   {
     fd = open(*(argv + 1),O_RDWR|O_CREAT,S_IRWXU); 
    //打開文件,如果沒有則創(chuàng)建
   }
   printf("The File Descriptor is %d\n",fd);	//打印文件描述符
   printf("Plz input the strings and use Enter as the end!\n");
   gets(writebuf);	                        //將終端輸入的數(shù)據(jù)寫入文件
   temp = write(fd,writebuf,strlen(writebuf));   //使用文件描述符調(diào)用文件
   printf("The input length is %d\n",temp);   
   close(fd);
   return 0;
 }


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI