【发布时间】:2013-03-03 22:58:32
【问题描述】:
void RecvFile()
{
int rval;
char buf[0x1000];
FILE *file = fopen("C:\\pic.bmp", "wb");
if (!file)
{
printf("Can't open file for writing");
return;
}
do
{
rval = recv(winsock, buf, sizeof(buf), 0);
if (rval < 0)
{
// if the socket is non-blocking, then check
// the socket error for WSAEWOULDBLOCK/EAGAIN
// (depending on platform) and if true then
// use select() to wait for a small period of
// time to see if the socket becomes readable
// again before failing the transfer...
printf("Can't read from socket");
fclose(file);
return;
}
if (rval == 0)
break; //line 159
int off = 0;
do
{
int written = fwrite(&buf[off], 1, rval - off, file);
if (written < 1)
{
printf("Can't write to file");
fclose(file);
return;
}
off += written;
}
while (off < rval)
} //line 175
fclose(file);
}
'}' 标记前的 175 个语法错误
159 被之前的错误弄糊涂了,想要摆脱困境
我不知道该怎么办...你能帮帮我吗? 我在 c 编程方面还是个新手。 我在代码中插入了错误行。 我不明白为什么会出现这个错误......你们能解释一下为什么吗?
【问题讨论】:
-
是的,我该如何解决?我不能直接删除它可以吗?我认为这是一个错误检查...
-
这是一种相当可怕的缩进 do-while 循环的方式...尝试将
do {或至少} while(...);作为一行,不要拆分它们。您可能会通过不同的缩进自己发现错误... -
谢谢你们……现在我明白为什么了……
-
OT:存储来自
read()/recv()和write()/send()的结果的首选类型是ssize_t。对于fread()和fwrite(),它是size_t。
标签: c sockets file-transfer