文件RecvFile函数中预期的令牌错误
Expected token error in file RecvFile function
我正在学习一些关于c++套接字的例子。这里的一个代码有一个错误:"expect token while got fclose"在最后一行
的上方代码看起来不错,所以我不知道这里出了什么问题。
欢迎大家多多指教。
void RecvFile(int sock, const char* filename)
{
int rval;
char buf[0x1000];
FILE *file = fopen(filename, "wb");
if (!file)
{
printf("Can't open file for writing");
return;
}
do
{
rval = recv(sock, 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;
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);
}
fclose(file);
}
您有一个do
而没有相应的while
:
do
{
// ...
do
{
// ...
}
while (off < rval);
}
// No while here
fclose(file);
它似乎应该只是while (true)
,你不妨只是粘在顶部,而不是做一个do while
。如果recv
返回0或更小,则执行将从循环中中断,这分别表示有序关闭和错误。所以改成:
while (true)
{
// ...
do
{
// ...
}
while (off < rval);
}
fclose(file);
您有一个do
语句而没有相应的while
:
do // <== THERE IS NO CORRESPONDING while FOR THIS do
{
rval = recv(sock, buf, sizeof(buf), 0);
if (rval < 0)
{
// ...
}
// ...
do
{
// ...
}
while (off < rval); // <== This is OK: the "do" has a matching "while"
}
// Nothing here! Should have a "while (condition)"
如果你只是想无限地重复你的循环,那么你应该使用while (true)
——要么替换do
关键字(最好),要么在缺失的while
应该去的地方添加它(如上面的注释所示)。
您在没有实际提供while();
的情况下启动了do
do
{
rval = recv(sock, 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;
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);
} //while() Needs to go here
fclose(file);
相关文章:
- holeMenuProgram.cpp:38:1 错误:'}'令牌之前的预期主表达式
- Arduino 用于语句错误。令牌之前的预期')' ';'。如何解决这个问题?
- 错误:令牌 { '{' 之前应存在非限定 ID
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- (错误:令牌之前'<<'预期的主表达式)
- 令牌 C++ 错误之前'['非限定 ID
- 修复令牌错误之前';'预期的非限定 ID
- 是什么导致了构造函数之后的这些"意外令牌"错误?
- 插入protobuf时SQLite无法识别的令牌错误:要处理哪些令牌以及如何处理?
- 获取语法和意外令牌错误,任何人都知道原因
- 令牌"("错误之前缺少 QT 二进制运算符
- 声明cudaError_t变量时出现无法识别的令牌错误
- CFtpConnection::GetCurrentDirectory未解析的令牌错误.不过,像SetCurrentDi
- 令牌错误之前"{"预期类名
- 令牌错误之前的预期主表达式'<'
- “{”令牌错误前预期的非限定id
- 令牌错误之前'{'预期类名
- 文件RecvFile函数中预期的令牌错误
- 令牌错误之前应为不合格的id
- Visual Studio 2010 + CUDA 4:尝试使用NPP分配内存时出现无法识别的令牌错误