从文件读取到矢量 C++ 的函数
Function reading from a file into vector C++
我编写了一个函数,可将未知数量的数据(在列中时)从文件读取到矢量。
#include <iostream>
#include <vector>
#include <fstream> // file writing
#include <cassert>
void ReadFromFile(std::vector<double> &x, const std::string &file_name)
{
std::ifstream read_file(file_name);
assert(read_file.is_open());
size_t lineCount = 0;
while (!read_file.eof())
{
double temp;
read_file >> temp;
x.at(lineCount) = temp;
if (lineCount == x.size() - 1) { break; } // fixes the out of range exception
lineCount++;
}
read_file.close();
}
int main()
{
size_t Nx = 7;
size_t Ny = 7;
size_t Nz = 7;
size_t N = Nx*Ny*Nz;
// Initial Arrays
std::vector <double> rx(N);
std::string Loadrx = "Loadrx.txt";
ReadFromFile(rx, Loadrx);
}
但是在将文件中的数据复制到向量中之后,lineCount 会额外增加一次。有没有比我写的 if 语句更优雅的方法来解决这个问题?
编辑:显然,我不会上传数据文件。代码编译完美我只是在寻找 if 语句的改进(如果存在的话)。
尝试:
void ReadFromFile(const size_t N,
std::vector<double> &x,
const std::string &file_name)
{
std::ifstream read_file(file_name);
assert(read_file.is_open());
while (true)
{
double temp;
read_file >> temp; // read something
if(read_file.eof()) break; // exit when eof
x.push_back(temp);
if (N == x.size()) break; // exit when N elements
}
read_file.close();
}
int main()
{
size_t N = 10;
std::vector<double> v;
v.reserve(N);
ReadFromFile(v,"Data.txt");
}
I have written a function that reads an unknown number of data (when in a column) from a file to a vector.
从 "column"(或其他常规格式的)文件中读取未知数据量的最优雅(我想也是惯用的)方法之一是使用 istream 迭代器:
void ReadFromFile(std::vector<double> &x, const std::string &file_name)
{
std::ifstream read_file(file_name);
assert(read_file.is_open());
std::copy(std::istream_iterator<double>(read_file), std::istream_iterator<double>(),
std::back_inserter(x));
read_file.close();
}
用法:
int main()
{
// Note the default constructor - we are constructing an initially empty vector.
std::vector<double> rx;
ReadFromFile(rx, "Loadrx.txt");
}
如果你想写一个 "safe" 版本,但要读取的元素数量有限,请使用 copy_if
:
void ReadFromFile(std::vector<double> &x, const std::string &file_name, unsigned int max_read)
{
std::ifstream read_file(file_name);
assert(read_file.is_open());
unsigned int cur = 0;
std::copy_if(std::istream_iterator<double>(read_file), std::istream_iterator<double>(),
std::back_inserter(x), [&](const double&) {
return (cur++ < max_read);
});
read_file.close();
}
用法很明显:
ReadFromFile(rx, Loadrx, max_numbers);
我编写了一个函数,可将未知数量的数据(在列中时)从文件读取到矢量。
#include <iostream>
#include <vector>
#include <fstream> // file writing
#include <cassert>
void ReadFromFile(std::vector<double> &x, const std::string &file_name)
{
std::ifstream read_file(file_name);
assert(read_file.is_open());
size_t lineCount = 0;
while (!read_file.eof())
{
double temp;
read_file >> temp;
x.at(lineCount) = temp;
if (lineCount == x.size() - 1) { break; } // fixes the out of range exception
lineCount++;
}
read_file.close();
}
int main()
{
size_t Nx = 7;
size_t Ny = 7;
size_t Nz = 7;
size_t N = Nx*Ny*Nz;
// Initial Arrays
std::vector <double> rx(N);
std::string Loadrx = "Loadrx.txt";
ReadFromFile(rx, Loadrx);
}
但是在将文件中的数据复制到向量中之后,lineCount 会额外增加一次。有没有比我写的 if 语句更优雅的方法来解决这个问题?
编辑:显然,我不会上传数据文件。代码编译完美我只是在寻找 if 语句的改进(如果存在的话)。
尝试:
void ReadFromFile(const size_t N,
std::vector<double> &x,
const std::string &file_name)
{
std::ifstream read_file(file_name);
assert(read_file.is_open());
while (true)
{
double temp;
read_file >> temp; // read something
if(read_file.eof()) break; // exit when eof
x.push_back(temp);
if (N == x.size()) break; // exit when N elements
}
read_file.close();
}
int main()
{
size_t N = 10;
std::vector<double> v;
v.reserve(N);
ReadFromFile(v,"Data.txt");
}
I have written a function that reads an unknown number of data (when in a column) from a file to a vector.
从 "column"(或其他常规格式的)文件中读取未知数据量的最优雅(我想也是惯用的)方法之一是使用 istream 迭代器:
void ReadFromFile(std::vector<double> &x, const std::string &file_name)
{
std::ifstream read_file(file_name);
assert(read_file.is_open());
std::copy(std::istream_iterator<double>(read_file), std::istream_iterator<double>(),
std::back_inserter(x));
read_file.close();
}
用法:
int main()
{
// Note the default constructor - we are constructing an initially empty vector.
std::vector<double> rx;
ReadFromFile(rx, "Loadrx.txt");
}
如果你想写一个 "safe" 版本,但要读取的元素数量有限,请使用 copy_if
:
void ReadFromFile(std::vector<double> &x, const std::string &file_name, unsigned int max_read)
{
std::ifstream read_file(file_name);
assert(read_file.is_open());
unsigned int cur = 0;
std::copy_if(std::istream_iterator<double>(read_file), std::istream_iterator<double>(),
std::back_inserter(x), [&](const double&) {
return (cur++ < max_read);
});
read_file.close();
}
用法很明显:
ReadFromFile(rx, Loadrx, max_numbers);