简体   繁体   English

fprintf不会编写整个过程

[英]fprintf does not write the whole process

I'm writing a code which takes 1024 packets from another program, prints them to the console and writes them into a txt file. 我正在编写一个代码,该代码从另一个程序获取1024个数据包,然后将它们打印到控制台,并将它们写入txt文件。 Now the packets are transmitted just fine and the receiver prints them to the console successfully, all 1024 packets. 现在,数据包可以很好地传输,接收器将所有1024个数据包成功打印到控制台。 However when I open the txt file, I see that it stopped writing in the middle of 972th packet. 但是,当我打开txt文件时,我看到它在第972个数据包中间停止写入。 My output on the txt is like this: 我在txt上的输出是这样的:

Binary Buffer1: 01111000110 :966

Binary Buffer2: 01111000111 :967

XOR:            00000000001


Binary Buffer1: 01111001000 :968

Binary Buffer2: 01111001001 :969

XOR:            00000000001


Binary Buffer1: 01111001010 :970

Binary Buffer2: 01111001011 :971

XOR:            00000000001


Binary Buffer1: 01111001100 :972

Bin

It just stops after that Bin. 它只是在那个Bin之后停止了。 My code part for fprintf is like this: 我的fprintf代码部分是这样的:

if(message_to_send[0]=='\0'&&message_to_send2[0]=='\0')

    {

        n = recvfrom(sockfd,message_to_send,5,0,(struct sockaddr *)&cliaddr,&len);

    sayi1=atoi(message_to_send);

    printf("Binary Buffer1: %s :%d\n",byte_to_binary(sayi1),sayi1);

    fprintf(f, "Binary Buffer1: %s :%d\n", byte_to_binary(sayi1),sayi1);

    sendto(sockfd,message_to_send,n,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));

    }


else if (message_to_send[0]!='\0'&&message_to_send2[0]=='\0')

    {

    n2 = recvfrom(sockfd,message_to_send2,5,0,(struct sockaddr *)&cliaddr,&len);

    sayi2=atoi(message_to_send2);

    printf("Binary Buffer2: %s :%d\n",byte_to_binary(sayi2),sayi2);

    fprintf(f, "Binary Buffer2: %s :%d\n", byte_to_binary(sayi2),sayi2);



    sendto(sockfd,message_to_send2,n2,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));

    }

This code block is in a loop so it prints all 1024 packets. 该代码块处于循环中,因此它打印所有1024个数据包。 I have no idea what might be the cause. 我不知道可能是什么原因。 There is no limit while writing into a text file right? 写入文本文件没有限制吗?

Thank you for your help. 谢谢您的帮助。

Do you fclose the file at the end of the program and is this fclose really reached? 您是否在程序结尾处关闭文件,并且确实达到了该关闭操作? Otherwise buffered file IO might not be written to the file. 否则,缓冲的文件IO可能不会写入该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM