实际读到的字节数

ffjj56 2011-05-26 07:41:13
unsigned int bytesRead=fread(pSaveH,sizeof(char),fileLen,fp);
文本大小78202 实读77362 差了800多,这个差值表示的是有800多个'\n'对吗?
...全文
112 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ffjj56 2011-05-26
  • 打赏
  • 举报
回复
太谢谢各位了,我也是没有rb读过文本呢
feimashenhua 2011-05-26
  • 打赏
  • 举报
回复
哦,12楼已经说了
feimashenhua 2011-05-26
  • 打赏
  • 举报
回复
fread 一般用二进制方式打开文件
c_losed 2011-05-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 yfkiss 的回复:]

LZ可以用rb打开
测试代码:
C/C++ code

FILE * pFile;
long lSize;
char * buffer;
size_t result;

pFile = fopen ( "test.txt" , "rb" );
if (pFile==NULL) {fputs ("File error",stderr); exi……
[/Quote]
++
lz把模式改下就能一次读完了
汗啊 木想到。。。
c_losed 2011-05-26
  • 打赏
  • 举报
回复

#include<string>
#include <tchar.h>

int main(int argc, _TCHAR* argv[])
{
FILE *fp=fopen("E:\\coffee\\MyTemp\\Price.txt","r+");
fseek(fp,0,SEEK_END);
int fileLen=ftell(fp);
char *pSaveH=new char[fileLen+1]; //(std::nothrow)
if(!pSaveH)
{
printf("pSaveH分配失败");
return 1;
}
memset(pSaveH,0,sizeof(char)*fileLen);

fseek(fp,0,SEEK_SET);
unsigned int bytesRead=fread(pSaveH,sizeof(char),fileLen,fp);
while (bytesRead != fileLen)
{
fseek(fp,bytesRead,SEEK_SET);
unsigned int bytesReadAgain = fread(pSaveH,sizeof(char),fileLen,fp);
bytesRead += bytesReadAgain;

}
fclose(fp);

printf("%s",pSaveH);
getchar();
return 0;
}
yfk 2011-05-26
  • 打赏
  • 举报
回复
LZ可以用rb打开
测试代码:

FILE * pFile;
long lSize;
char * buffer;
size_t result;

pFile = fopen ( "test.txt" , "rb" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}

// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);

cout << lSize << endl;
rewind (pFile);

// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);

cout << result << endl;

if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

/* the whole file is now loaded in the memory buffer. */

// terminate
fclose (pFile);
free (buffer);

getchar();

return 0;
c_losed 2011-05-26
  • 打赏
  • 举报
回复
确实木读完 用循环判断继续读吧
文件大小78202字节
读取了77362字节
ffjj56 2011-05-26
  • 打赏
  • 举报
回复
下面清晰些,仍是在最后多出来无意义的6行

#include "stdafx.h"
#include<string>

int _tmain(int argc, _TCHAR* argv[])
{
FILE *fp=fopen("E:\\coffee\\MyTemp\\Price.txt","r+");
fseek(fp,0,SEEK_END);
int fileLen=ftell(fp);
char *pSaveH=new(std::nothrow) char[fileLen+1];
if(!pSaveH)
{
printf("pSaveH分配失败");
return 1;
}
memset(pSaveH,0,sizeof(char)*fileLen);

fseek(fp,0,SEEK_SET);
unsigned int bytesRead=fread(pSaveH,sizeof(char),fileLen,fp);
fclose(fp);

printf("%s",pSaveH);
getchar();
return 0;
}
ffjj56 2011-05-26
  • 打赏
  • 举报
回复

// console.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<string>

int _tmain(int argc, _TCHAR* argv[])
{
FILE *fp=fopen("E:\\coffee\\MyTemp\\Price.txt","r+");
fseek(fp,0,SEEK_END);
int fileLen=ftell(fp);
int len=800+fileLen;
char *pFruit=new(std::nothrow) char[len];
if(!pFruit){printf("pFruit分配失败"); return 1;}
memset(pFruit,0,sizeof(char)*len);

char *pSaveH=new(std::nothrow) char[fileLen+1];
if(!pSaveH) {delete[] pFruit;printf("pSaveH分配失败"); return 1;}
memset(pSaveH,0,sizeof(char)*fileLen);

fseek(fp,0,SEEK_SET);
unsigned int bytesRead=fread(pSaveH,sizeof(char),fileLen,fp);
fclose(fp);
if( pSaveH[strlen(pSaveH)-1]!='\n' || pSaveH[strlen(pSaveH)-2]=='\n' )
{//总LF个数=fileLen-strlen=(text+LF*2)-(text+LF*1)
printf("Price.txt结尾只能是1个\\n");
delete[] pSaveH;delete[] pFruit;
return 1;
}
return 0;
}

从很长的文件里取出来的有些复合语句写到同一行了
文本仍是读不全,调试时监视pSaveH会发现最后那800个字节左右和Price.txt文件里的内容有出入
Price.txt文本有是77KB的 找了个地方传上来啦: http://good.gd/1222807.htm
ffjj56 2011-05-26
  • 打赏
  • 举报
回复
谢谢,请稍等,我整理下
c_losed 2011-05-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ffjj56 的回复:]

回楼上的,调试也没有用,显然是不明原因的少读了,ferror(fp)均返回0
[/Quote]
方便发全码不 偶调试下
ffjj56 2011-05-26
  • 打赏
  • 举报
回复
回楼上的,调试也没有用,显然是不明原因的少读了,ferror(fp)均返回0
c_losed 2011-05-26
  • 打赏
  • 举报
回复
debug 看内存地址 对比文件内容
ffjj56 2011-05-26
  • 打赏
  • 举报
回复
出现的差值是怎么回事啊
xspace_time 2011-05-26
  • 打赏
  • 举报
回复
不明白,不读\n怎么知道有个\n

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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