[英]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.