使用ifstream的read()问题

dnagent 2009-03-03 10:49:00
使用ifstream的read()来读取一个文本文件。
我特意把tempBuf的大小设置为4096,使用size来控制读取缓冲区的大小。
但是当文本内容超过4096的时候,read()还是会一股脑的全塞到tempBuf里,造成内存越界,这是怎么回事?


int size = 4096;
//char buf[4096];
char *tempBuf = new char(size);
//memset(tempBuf,0,size);
ifstream inFile;
inFile.open("C:\\DllText.txt");

if(inFile)
{
inFile.read(tempBuf,size);
}
cout << strlen(tempBuf) <<endl;
cout << tempBuf << endl;

inFile.close();
...全文
2613 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Alionkun 2009-11-13
  • 打赏
  • 举报
回复
学习了
dnagent 2009-03-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yangch_nhcmo 的回复:]
C/C++ code#include<iostream>#include<fstream>usingnamespacestd;intmain()
{intsize=4096;//char buf[4096];char*tempBuf=newchar[size+1];//memset(tempBuf,0,size);ifstream inFile;
inFile.open("C:\\DllText.txt");if(inFile)
{
inFile.read(tempBuf,size);
}
cout<<strlen(tempBuf)<<endl;//tempBuf[size] = '\0';/*不是截断,cout << tempBuf << endl;这句是把tempBuf作为一个字符串来输出,tempBuf是个…
[/Quote]

非常感谢
忘了输出的时候必须检测'\0'了
yangch_nhcmo 2009-03-03
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
int size = 4096;
//char buf[4096];
char *tempBuf = new char[size + 1 ];
//memset(tempBuf,0,size);
ifstream inFile;
inFile.open("C:\\DllText.txt");

if (inFile)
{
inFile.read(tempBuf,size);
}
cout << strlen(tempBuf) <<endl;

// tempBuf[size] = '\0';
/*
不是截断,cout << tempBuf << endl;这句是把tempBuf作为一个字符串来输出,tempBuf是个char类型,要手动添加字符串结束符,你改成下面这样也是一样的
*/
tempBuf[strlen(tempBuf)] = '\0';

cout << tempBuf << endl;
inFile.close();

return 0;
}
dnagent 2009-03-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yangch_nhcmo 的回复:]
C/C++ code#include<iostream>#include<fstream>usingnamespacestd;intmain()
{intsize=4096;//char buf[4096];char*tempBuf=newchar[size+1];//update this line//memset(tempBuf,0,size);ifstream inFile;
inFile.open("C:\\DllText.txt");if(inFile)
{
inFile.read(tempBuf,size);
}
cout<<strlen(tempBuf)<<endl;

tempBuf[size]='\0';//add this linecout<<tempBuf<<endl;
inFile.close();return0;
}
[/Quote]

呵呵,很暴力,这样是强行截断了吗
yangch_nhcmo 2009-03-03
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
int size = 4096;
//char buf[4096];
char *tempBuf = new char[size + 1 ]; //update this line
//memset(tempBuf,0,size);
ifstream inFile;
inFile.open("C:\\DllText.txt");

if (inFile)
{
inFile.read(tempBuf,size);
}
cout << strlen(tempBuf) <<endl;

tempBuf[size] = '\0'; // add this line
cout << tempBuf << endl;
inFile.close();

return 0;
}
dnagent 2009-03-03
  • 打赏
  • 举报
回复
莫非因为文本内容是中文造成的?
dnagent 2009-03-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lightnut 的回复:]
char *tempBuf = new char(size);
=====>
char *tempBuf = new char[size];

[/Quote]

对,这个地方错了,现在没有报错了
但是strlen(tempBuf)超过4096,达到4100.

但是问题还是存在,我问本里存了超过4096大小的内容,要求read(tempBuf,size),size是4096的内容。
相当于读多了,我只要读4096,但是全给读出来了
xudeng22 2009-03-03
  • 打赏
  • 举报
回复
同意楼上

lightnut 2009-03-03
  • 打赏
  • 举报
回复
char *tempBuf = new char(size);
=====>
char *tempBuf = new char[size];

33,311

社区成员

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

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