简体   繁体   English

fwrite() 没有完成写入文件

[英]fwrite() doesn't finish writing to file

Trying to send a file from client to server.试图将文件从客户端发送到服务器。

This is how I receive it:这是我收到它的方式:

printf("Server receiving file...\n");
while((bytes_read = read(sd, buf, sizeof(buf))) > 0){ //second read now retrieving the file size
  printf("writing");
  fwrite(buf, 1, bytes_read, fp);
  printf("checking if reached here");
}

For some reason, the second print statement "checking if reached here" is never reached, and the program just doesn't exit the loop.出于某种原因,永远不会到达第二个打印语句“检查是否到达此处”,并且程序只是没有退出循环。 What could be the issue?可能是什么问题?

PS.附注。 My client sends all the bytes so there is no issue with sending.我的客户端发送所有字节,因此发送没有问题。 Its only the writing to the file它只是写入文件

Is the socket blocking (the default) ?套接字是否阻塞(默认)?

If it is, whenever there is no data to read, the read() will hang, waiting for more data (or for the socket to close, in which case it will return 0 bytes).如果是,则每当没有数据要读取时,read() 将挂起,等待更多数据(或等待套接字关闭,在这种情况下它将返回 0 字节)。

Did you open the file using the fopen function?您是否使用 fopen 函数打开文件? Here is a small example regarding a piece of code you should have somewhere in your script.这是一个关于您应该在脚本中某处包含的一段代码的小示例。

fp = fopen( "file", "w" );

Check if you have a similar line somewhere, otherwise you will need to add this.检查您是否在某处有类似的行,否则您需要添加它。

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

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