[英]Using O_CREAT O_WRONLY : How to get current position in the file and append data at the end?
我想將數據從套接字讀取到FILE中。 如果FILE不存在,則僅創建一次文件和來自客戶端的任何后續數據,找到FILE POINTER的當前位置和append末尾的數據。
現在,每次運行代碼時都會創建一個新文件。
// Server is 'Ready' to read data from the socket :
// fd - declared as **static int**, to enable it bet'n the function calls
fd = open("/home/regs_p/cprograms/tcp/RSA.c", O_WRONLY | O_APPEND | O_CREAT | O_TRUNC);
if (fd < 0) {
printf("Some problem with the file!");
}
else {
while ((b = read(sockfd, buffer, sizeof(buffer) - 1)) > 0) {
if (fd > 0 ) {
fp = fdopen(fd, "a+");
fwrite(buffer, sizeof(buffer), 1, fp);
// fseek(fp, 0, SEEK_CUR);
}
size = ftell(fp);
}
// printf("Buffer = %s", buffer);
}
僅當文件不存在時,將O_APPEND
與O_CREAT
一起使用才會創建文件。 如果存在,則打開文件,默認情況下文件指針位於末尾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.