//這是在上一個實(shí)例的基礎(chǔ)上用strcat函數(shù)解決了回車換行的問題
#include
#include
#include
int main(int argc,char *argv[])
{
int fd; //文件描述符
int temp; //臨時變量
char writebuf[30]; //用于存放寫入字符串
char endbuf[] = "\n"; //存放一個回車換行符號
if(argc != 2) //如果參考字符串錯誤
{
printf("Plz input the correct file name as 'exam308WriteFun 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!\n");
gets(writebuf); //將終端輸入的數(shù)據(jù)寫入文件
strcat(writebuf,endbuf); //添加換行符
temp = write(fd,writebuf,strlen(writebuf)); //使用文件描述符調(diào)用文件
printf("The input length is %d\n",temp);
close(fd);
return 0;
}
分享名稱:[Linux文件]帶回車換行的寫入字符串實(shí)例
文章URL:
http://weahome.cn/article/jecohs.html