我正在寫一份dup()
函數(我正在爲Linux api學習一本書)。open()using O_APPEND does not update EOF
我有一個名爲temp.txt
的文件,它包含一行以下字符串:Hello, World
。
下面是代碼:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
//NAIVE IMPLEMENTATION
int dup_t(int old) {
if(old == -1) {
errno = EBADF;
return -1;
}
int flags;
flags = fcntl(old, F_GETFL);
if(flags == - 1) {
errno = EBADF;
return -1;
}
int new;
if((new = fcntl(old, F_DUPFD)) == - 1) {
errno = EBADF;
return - 1;
}
return new;
}
int main(int argc, char *argv[]) {
if(argc == 1) {
printf("Error, no arguments given\n");
exit(-1);
}
int fd, cpfd;
if((fd = open(&argv[1][0], O_RDWR | O_APPEND)) == -1) {
printf("Error opening the file\n");
exit(-1);
}
cpfd = dup_t(fd);
if(cpfd == -1) {
printf("Error dup_t()\n");
exit(-1);
}
if(close(fd) == -1) {
printf("Error closing fd\n");
exit(-1);
}
if(write(cpfd, "kostas", 6) == - 1) {
printf("Error writting\n");
exit(-1);
}
if(close(fd) == -1) {
printf("Error closing fd\n");
exit(-1);
}
if(write(cpfd, "kostas", 6) == - 1) {
printf("Error writting\n");
exit(-1);
}
if(close(cpfd) == -1) {
printf("Error closing cpfd\n");
exit(-1);
}
}
所以,通過運行./prog temp.txt
成功,文件temp.txt
必須包含以下字符串。 Hello, WorldKostas
通過運行以下命令cat temp.txt
輸出我得到的是Hello, World
,但是,如果我打開像nano
文本編輯器中的文件,我得到Hello, World
(後跟其中包含一個新行)kostas
。
爲什麼cat
命令會產生錯誤的輸出? 爲什麼在字符串Hello, World
的末尾添加了新行?
我不是在尋找解決方法,我有興趣找出錯誤/問題的原因。
您可以創建您的初始溫度。txt文件,不包含使用'echo -n「Hello,World」> temp.txt'的行尾字符。 –
'EOF'是C標準庫函數使用的一個宏,不是一個標誌,也不是真正存儲在文件中的,也不是Linux API的一部分(因此'''''''''''''''''''''''''''''''')。 – Olaf
@ KostasRim:1)不要假設誰的評論也是一個downvoter。 2)即使用戶是否自己決定是否將您的問題視爲DV候選人。無論哪種方式,向一位評論者抱怨DV都會產生反作用:如果他是,他可能下次不會發表評論,但是會默默地DV;我不認爲這是你真正想要的。否則,DVer將不會閱讀投訴,並且評論者可能會僅僅因爲誣告而考慮DV。只是我的想法。 – Olaf