在写二进制文件中的一个问题.50分!

holy198048 2004-03-02 11:16:57
我使用ofstream 进行写文件,我规定写入数据是5260,可文件大小是5270??
十分奇怪,百思不得其解。我写入普通文本文件,大小是规定的大小。
程序片断: 是从SOCKET中读取数据,写入文件中的。
if (bOnce) {
nRet = ReceiveTxt(mesg, MAX_LENGTH);
if (-1 == nRet) {
cout << " receive message failed " << endl;
return false;
}
nLen = GetFileLength(mesg);
cout << " File Length: "<< nLen << endl;
nFileLen = nLen; // assign to class value nFileLen;
string file = mesg;
int nFind = file.find("Content-Length:");
nFind = nFind + strlen("Content-Length:");
char temp[MAX_LENG];

itoa(nLen, temp, 10);
nFind = nFind + strlen(temp);
nRecv = nRet - nFind - 5;

if (nRecv == 0) {
bOnce = false;
continue;
}
char *txt = mesg;
txt = txt + nFind + 5; // why need to add 5;

if (txt == NULL ) {
cout << "write message is null " << endl;
return false;
}
outfile.write(txt, nRecv);
nLen = nLen - nRecv;
if (nLen == 0) {
return true;
}

outfile.write(txt, nRecv);问题在此,我规定写入nRecv数据大小,可是文件比其大10多个字节,是二进制文件,不便于比较大小
...全文
90 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧