我正在编写udp协议代码,服务器端的功能之一是将接收到的字符串写入输出的.txt文件中。
这是我关于此问题的代码的一部分
for (;;)
{
fpout = fopen("out.txt","w");
if(fpout == NULL){
printf("Error!");
exit(1);
}
int receive = 1;
while(receive)
{
bytes_recd = recvfrom(sock_server, &pkt, STRING_SIZE, 0, (struct sockaddr *) &client_addr, &client_addr_len);
if(pkt.length == 0)
receive = 0;
printf("Server received Sentence is: %s\n with length of %d\n", pkt.databytes, pkt.length);
printf("Server received %d packet\n",pkt.seqnum);
strcpy(sentence, pkt.databytes);
fprintf(fpout, "%s", sentence);
}
fclose(fpout);
}
pkt是我传输的结构,pkt.databytes是我要写入.txt文件的字符串部分。
但是现在,没有任何内容写入out.txt文件。
最佳答案
谢谢你们,它有效。我想指出的是,我想念NUL终止了,我在fopen中也将“ a”而不是“ w”更改了。再次感谢。 – EricBkc