问题描述
我想了解提供的答案,但我似乎无法使用。
/ p>
这是我试过的:
#include<算法>
#include< iostream>
#include< iterator>
#include< vector>
#include< fstream>
int main()
{
std :: string path(numbersfile);
std :: vector< int> myVector {1,16,32,64};
std :: vector< int> newVector {};
std :: ofstream FILE(path,std :: ios :: out | std :: ofstream :: binary);
std :: copy(myVector.begin(),myVector.end(),std :: ostreambuf_iterator< char>(FILE));
std :: ifstream INFILE(path,std :: ios :: in | std :: ifstream :: binary);
std :: istreambuf_iterator< char> iter(INFILE);
//std::copy(iter.begin(),iter.end(),std::back_inserter(newVector)); // this does not compile
std :: copy(iter,std :: istreambuf_iterator< char> {},std :: back_inserter(newVector)); // this leaves newVector empty
}
newVector
在最后的复制
后仍然为空。如何更新最后一条语句以填充 newVector
?
文件未准备好在第二个复制
被调用的时候读取。 (感谢Piotr Skotnicki为他在回应中的回答)
呼叫允许程序工作:
#include< algorithm>
#include< iostream>
#include< iterator>
#include< vector>
#include< fstream>
int main()
{
std :: string path(numbersfile);
std :: vector< int> myVector {1,16,32,64};
std :: vector< int> newVector {};
std :: ofstream FILE(path,std :: ios :: out | std :: ofstream :: binary);
std :: copy(myVector.begin(),myVector.end(),std :: ostreambuf_iterator< char>(FILE));
FILE.flush(); // required here
std :: ifstream INFILE(path,std :: ios :: in | std :: ifstream :: binary);
std :: istreambuf_iterator< char> repeat(INFILE);
//std::copy(iter.begin(),iter.end(),std::back_inserter(newVector)); // this does not compile
std :: copy(iter,std :: istreambuf_iterator< char> {},std :: back_inserter(newVector)); // this leaves newVector empty
return 0;
}
ofstream
仍然在创建 ifstream
时仍在作用域中。如果 ofstream
的析构函数被调用,那么该文件也可以准备好 ifstream
。在下面的程序中, ifstream
会自动被破坏:
#include< ;算法>
#include< fstream>
#include< iterator>
#include< vector>
std :: string filename(numbersfile);
std :: vector< char> myVector {1,16,32,64};
void write_vector_to_file(const std :: vector< char>& myVector,std :: string filename);
std :: vector< char> read_vector_from_file(std :: string filename);
int main()
{
write_vector_to_file(myVector,filename);
auto newVector {read_vector_from_file(filename)};
return 0;
}
void write_vector_to_file(const std :: vector< char>& myVector,std :: string filename)
{
std :: ofstream ofs ,std :: ios :: out | std :: ofstream :: binary);
std :: ostream_iterator< char> osi {ofs};
std :: copy(myVector.begin(),myVector.end(),osi);
}
std :: vector< char> read_vector_from_file(std :: string filename)
{
std :: vector< char> newVector {};
std :: Ifstream ifs(filename,std :: ios :: in | std :: ifstream :: binary);
std :: istreambuf_iterator< char> iter(ifs);
std :: istreambuf_iterator< char>结束{};
std :: copy(iter,end,std :: back_inserter(newVector));
return newVector;
}
I'm trying to understand the answer provided here, but I can't seem to make it work.
Here is what I've tried:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <fstream>
int main()
{
std::string path("numbersfile");
std::vector<int> myVector{1,16,32,64};
std::vector<int> newVector{};
std::ofstream FILE(path,std::ios::out | std::ofstream::binary);
std::copy(myVector.begin(),myVector.end(),std::ostreambuf_iterator<char>(FILE));
std::ifstream INFILE(path,std::ios::in | std::ifstream::binary);
std::istreambuf_iterator<char> iter(INFILE);
//std::copy(iter.begin(),iter.end(),std::back_inserter(newVector)); //this doesn't compile
std::copy(iter,std::istreambuf_iterator<char>{},std::back_inserter(newVector)); // this leaves newVector empty
}
newVector
is still empty after the last copy
. How could the last statement be updated to populate newVector
?
The file is not ready to be read by the time the second copy
is called. (Thanks to Piotr Skotnicki for his answer in the comments)
A call to flush
allows the program to work:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <fstream>
int main()
{
std::string path("numbersfile");
std::vector<int> myVector{1,16,32,64};
std::vector<int> newVector{};
std::ofstream FILE(path,std::ios::out | std::ofstream::binary);
std::copy(myVector.begin(),myVector.end(),std::ostreambuf_iterator<char>(FILE));
FILE.flush(); // required here
std::ifstream INFILE(path,std::ios::in | std::ifstream::binary);
std::istreambuf_iterator<char> iter(INFILE);
//std::copy(iter.begin(),iter.end(),std::back_inserter(newVector)); //this doesn't compile
std::copy(iter,std::istreambuf_iterator<char>{},std::back_inserter(newVector)); // this leaves newVector empty
return 0;
}
The ofstream
is still in scope when the ifstream
is created. Had the ofstream
's destructor been called then the file would also have been ready for the ifstream
. In the following program the ifstream
is automatically destructed:
#include <algorithm>
#include <fstream>
#include <iterator>
#include <vector>
std::string filename("numbersfile");
std::vector<char> myVector{1,16,32,64};
void write_vector_to_file(const std::vector<char>& myVector, std::string filename);
std::vector<char> read_vector_from_file(std::string filename);
int main()
{
write_vector_to_file(myVector, filename);
auto newVector{read_vector_from_file(filename)};
return 0;
}
void write_vector_to_file(const std::vector<char>& myVector,std::string filename)
{
std::ofstream ofs(filename,std::ios::out | std::ofstream::binary);
std::ostream_iterator<char> osi{ofs};
std::copy(myVector.begin(),myVector.end(),osi);
}
std::vector<char> read_vector_from_file(std::string filename)
{
std::vector<char> newVector{};
std::ifstream ifs(filename,std::ios::in | std::ifstream::binary);
std::istreambuf_iterator<char> iter(ifs);
std::istreambuf_iterator<char> end{};
std::copy(iter,end,std::back_inserter(newVector));
return newVector;
}
这篇关于使用迭代器正确地读取和写入std :: vector到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!