C++ 使用fstream读取文件
当我尝试将文件读取到缓冲区时,它总是在缓冲区的末尾附加随机字符C++ 使用fstream读取文件,c++,file-io,fstream,C++,File Io,Fstream,当我尝试将文件读取到缓冲区时,它总是在缓冲区的末尾附加随机字符 char* thefile; std::streampos size; std::fstream file(_file, std::ios::in | std::ios::ate); if (file.is_open()) { size = file.tellg(); std::cout << "size: " << size;
char* thefile;
std::streampos size;
std::fstream file(_file, std::ios::in | std::ios::ate);
if (file.is_open())
{
size = file.tellg();
std::cout << "size: " << size;
thefile = new char[size]{0};
file.seekg(0, std::ios::beg);
file.read(thefile, size);
std::cout << thefile;
}
int x = 0;
char*文件;
std::streampos大小;
std::fstream文件(_文件,std::ios::in | std::ios::ate);
if(file.is_open())
{
size=file.tellg();
std::cout以下是阅读您的收藏的更好方法:
#include <vector>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <cstdint>
#include <iterator>
template<class T>
void Write(std::string const & path, T const & value, std::ios_base::openmode mode)
{
if (auto stream = std::ofstream(path, mode))
{
Write(stream, value);
stream.close();
}
else
{
throw std::runtime_error("failed to create/open stream");
}
}
template<class T>
void Write(std::ostream & stream, T const & value)
{
std::copy(value.begin(), value.end(), std::ostreambuf_iterator<char>(stream));
if (!stream)
{
throw std::runtime_error("failed to write");
}
}
template<class T>
void Read(std::istream & stream, T & output)
{
auto eof = std::istreambuf_iterator<char>();
output = T(std::istreambuf_iterator<char>(stream), eof);
if(!stream)
{
throw std::runtime_error("failed to read stream");
}
}
template<class T>
void Read(std::string const & path, T & output)
{
if (auto stream = std::ifstream(path, std::ios::in | std::ios::binary))
{
Read(stream, output);
stream.close();
}
else
{
throw std::runtime_error("failed to create stream");
}
}
int main(void)
{
// Write and read back text.
{
auto const s = std::string("I'm going to write this string to a file");
Write("temp.txt", s, std::ios_base::trunc | std::ios_base::out);
auto t = std::string();
Read("temp.txt", t);
}
// Write and read back a set of ints.
{
auto const v1 = std::vector<int>() = { 10, 20, 30, 40, 50 };
Write("temp.txt", v1, std::ios_base::trunc | std::ios_base::out | std::ios_base::binary);
auto v2 = std::vector<int>();
Read("temp.txt", v2);
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
样板
无效写入(std::字符串常量和路径、T常量和值、std::ios\U base::openmode模式)
{
if(自动流=标准::of流(路径、模式))
{
写入(流、值);
stream.close();
}
其他的
{
抛出std::runtime_错误(“创建/打开流失败”);
}
}
样板
无效写入(标准::ostream&stream、T const&value)
{
std::copy(value.begin()、value.end()、std::ostreambuf_迭代器(流));
如果(!流)
{
抛出std::runtime_错误(“写入失败”);
}
}
样板
无效读取(标准::istream&stream,T&output)
{
自动eof=std::istreambuf_迭代器();
输出=T(标准::istreambuf_迭代器(流),eof);
如果(!流)
{
抛出std::runtime_错误(“读取流失败”);
}
}
样板
无效读取(标准::字符串常量和路径、T和输出)
{
if(自动流=std::ifstream(路径,std::ios::in | std::ios::binary))
{
读取(流、输出);
stream.close();
}
其他的
{
抛出std::runtime_错误(“未能创建流”);
}
}
内部主(空)
{
//写和读回文本。
{
auto const s=std::string(“我将把这个字符串写入文件”);
写入(“temp.txt”,s,std::ios|u base::trunc | std::ios|u base::out);
自动t=std::string();
读取(“temp.txt”,t);
}
//写入并读回一组整数。
{
auto const v1=std::vector()={10,20,30,40,50};
写入(“temp.txt”,v1,std::ios|u base::trunc | std::ios|u base::out | std::ios|u base::binary);
auto v2=std::vector();
读取(“temp.txt”,v2);
}
返回0;
}
使用易读容器,而不是使用“新的”。以下是阅读收藏的更好方法:
#include <vector>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <cstdint>
#include <iterator>
template<class T>
void Write(std::string const & path, T const & value, std::ios_base::openmode mode)
{
if (auto stream = std::ofstream(path, mode))
{
Write(stream, value);
stream.close();
}
else
{
throw std::runtime_error("failed to create/open stream");
}
}
template<class T>
void Write(std::ostream & stream, T const & value)
{
std::copy(value.begin(), value.end(), std::ostreambuf_iterator<char>(stream));
if (!stream)
{
throw std::runtime_error("failed to write");
}
}
template<class T>
void Read(std::istream & stream, T & output)
{
auto eof = std::istreambuf_iterator<char>();
output = T(std::istreambuf_iterator<char>(stream), eof);
if(!stream)
{
throw std::runtime_error("failed to read stream");
}
}
template<class T>
void Read(std::string const & path, T & output)
{
if (auto stream = std::ifstream(path, std::ios::in | std::ios::binary))
{
Read(stream, output);
stream.close();
}
else
{
throw std::runtime_error("failed to create stream");
}
}
int main(void)
{
// Write and read back text.
{
auto const s = std::string("I'm going to write this string to a file");
Write("temp.txt", s, std::ios_base::trunc | std::ios_base::out);
auto t = std::string();
Read("temp.txt", t);
}
// Write and read back a set of ints.
{
auto const v1 = std::vector<int>() = { 10, 20, 30, 40, 50 };
Write("temp.txt", v1, std::ios_base::trunc | std::ios_base::out | std::ios_base::binary);
auto v2 = std::vector<int>();
Read("temp.txt", v2);
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
样板
无效写入(std::字符串常量和路径、T常量和值、std::ios\U base::openmode模式)
{
if(自动流=标准::of流(路径、模式))
{
写入(流、值);
stream.close();
}
其他的
{
抛出std::runtime_错误(“创建/打开流失败”);
}
}
样板
无效写入(标准::ostream&stream、T const&value)
{
std::copy(value.begin()、value.end()、std::ostreambuf_迭代器(流));
如果(!流)
{
抛出std::runtime_错误(“写入失败”);
}
}
样板
无效读取(标准::istream&stream,T&output)
{
自动eof=std::istreambuf_迭代器();
输出=T(标准::istreambuf_迭代器(流),eof);
如果(!流)
{
抛出std::runtime_错误(“读取流失败”);
}
}
样板
无效读取(标准::字符串常量和路径、T和输出)
{
if(自动流=std::ifstream(路径,std::ios::in | std::ios::binary))
{
读取(流、输出);
stream.close();
}
其他的
{
抛出std::runtime_错误(“未能创建流”);
}
}
内部主(空)
{
//写和读回文本。
{
auto const s=std::string(“我将把这个字符串写入文件”);
写入(“temp.txt”,s,std::ios|u base::trunc | std::ios|u base::out);
自动t=std::string();
读取(“temp.txt”,t);
}
//写入并读回一组整数。
{
auto const v1=std::vector()={10,20,30,40,50};
写入(“temp.txt”,v1,std::ios|u base::trunc | std::ios|u base::out | std::ios|u base::binary);
auto v2=std::vector();
读取(“temp.txt”,v2);
}
返回0;
}
传入一个iterable容器,而不是使用“new”。如果文件未使用ios::binary
模式打开,则不能假定tellg()
返回的位置将为您提供要读取的字符数。文本模式操作可能会对流执行一些转换(f.ex:在windows上,它会将文件中的“\r\n”转换为“\n”,因此您可能会发现大小为2,但只读为1)
无论如何,read()
不会添加空终止符
最后,由于必须添加空终止符,您必须比预期的大小多分配一个字符。否则,在添加缓冲区时可能会发生缓冲区溢出
您应该验证实际使用了多少个字符,并相应地为字符串设置空终止符
thefile = new char[size + 1]{0}; // one more for the trailing null
file.seekg(0, std::ios::beg);
if (file.read(thefile, size))
thefile[size]=0; // successfull read: all size chars were read
else thefile[file.gcount()]=0; // or less chars were read due to text mode
如果文件不是用ios::binary
模式打开的,则不能假定tellg()
返回的位置将给出要读取的字符数。文本模式操作可能会对流执行一些转换(例如:在windows上,它将在“\n”中的文件中转换“\r\n”,因此您可能会发现大小为2,但只读为1)
无论如何,read()
不会添加空终止符
最后,由于必须添加空终止符,您必须比预期的大小多分配一个字符。否则,在添加缓冲区时可能会发生缓冲区溢出
您应该验证实际使用了多少个字符,并相应地为字符串设置空终止符
thefile = new char[size + 1]{0}; // one more for the trailing null
file.seekg(0, std::ios::beg);
if (file.read(thefile, size))
thefile[size]=0; // successfull read: all size chars were read
else thefile[file.gcount()]=0; // or less chars were read due to text mode
<>从C++ DOCs:< /P>
此函数只复制一块数据,不检查其内容,也不在末尾附加空字符
因此,您的字符串遗漏了表示字符串结尾的尾随空字符。在这种情况下,cout
将继续打印内存中文件
之外的字符
在字符串结尾添加一个“代码>”0”/代码> .< /P> < P>从C++ DOCs:<