[英]Read Numeric Data from a Text File in C++
例如,如果外部文本文件中的数据是这样的:
45.78 67.90 87
34.89 346 0.98
如何读取此文本文件并将每个数字分配给 C++ 中的变量? 使用 ifstream,我可以打开文本文件并将第一个数字分配给变量,但我不知道如何读取空格后的下一个数字。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
float a;
ifstream myfile;
myfile.open("data.txt");
myfile >> a;
cout << a;
myfile.close();
system("pause");
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int data[6], a, b, c, d, e, f;
ifstream myfile;
myfile.open("a.txt");
for(int i = 0; i << 6; i++)
myfile >> data[i];
myfile.close();
a = data[0];
b = data[1];
c = data[2];
d = data[3];
e = data[4];
f = data[5];
cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" << f << "\n";
system("pause");
return 0;
}
重复 >> 循环读取。
#include <iostream>
#include <fstream>
int main(int argc, char * argv[])
{
std::fstream myfile("D:\\data.txt", std::ios_base::in);
float a;
while (myfile >> a)
{
printf("%f ", a);
}
getchar();
return 0;
}
结果:
45.779999 67.900002 87.000000 34.889999 346.000000 0.980000
如果您确切知道文件中有多少个元素,您可以链接 >> 运算符:
int main(int argc, char * argv[])
{
std::fstream myfile("D:\\data.txt", std::ios_base::in);
float a, b, c, d, e, f;
myfile >> a >> b >> c >> d >> e >> f;
printf("%f\t%f\t%f\t%f\t%f\t%f\n", a, b, c, d, e, f);
getchar();
return 0;
}
编辑:回应您对主要问题的评论。
你有两个选择。
编辑:如何跳过文件中的值
要选择第 1234 个值,请使用以下代码:
int skipped = 1233;
for (int i = 0; i < skipped; i++)
{
float tmp;
myfile >> tmp;
}
myfile >> value;
它可能取决于,尤其是您的文件在每行上是否具有相同数量的项目。 如果可以,那么您可能需要某种 2D 矩阵类,通常是这样的:
class array2D {
std::vector<double> data;
size_t columns;
public:
array2D(size_t x, size_t y) : columns(x), data(x*y) {}
double &operator(size_t x, size_t y) {
return data[y*columns+x];
}
};
请注意,正如所写的那样,这假设您预先知道需要的大小。 这可以避免,但代码会变得更大更复杂。
在任何情况下,为了读取数字并保持原始结构,您通常一次将一行读入一个字符串,然后使用 stringstream 从该行读取数字。 这使您可以将每行中的数据存储到数组中的单独行中。
如果您事先不知道大小,或者(特别是)如果不同的行可能不都包含相同数量的数字:
11 12 13
23 34 56 78
您可能想使用std::vector<std::vector<double> >
代替。 这确实会带来一些开销,但如果不同的行可能具有不同的大小,这是完成这项工作的一种简单方法。
std::vector<std::vector<double> > numbers;
std::string temp;
while (std::getline(infile, temp)) {
std::istringstream buffer(temp);
std::vector<double> line((std::istream_iterator<double>(buffer)),
std::istream_iterator<double>());
numbers.push_back(line);
}
...或者,使用现代 (C++11) 编译器,您可以使用方括号进行line
的初始化:
std::vector<double> line{std::istream_iterator<double>(buffer),
std::istream_iterator<double>()};
number 的输入运算符跳过前导空格,因此您可以在循环中读取数字:
while (myfile >> a)
{
// ...
}
你可以像其他人一样单独读写。 但是如果你想写入同一个,你可以试试这个:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
double data[size of your data];
std::ifstream input("file.txt");
for (int i = 0; i < size of your data; i++) {
input >> data[i];
std::cout<< data[i]<<std::endl;
}
}
您可以使用2D vector
来存储您从文本文件中读取的数字,如下所示:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include<fstream>
int main() {
std::string line;
double word;
std::ifstream inFile("data.txt");
//create/use a std::vector
std::vector<std::vector<double>> vec;
if(inFile)
{
while(getline(inFile, line, '\n'))
{
//create a temporary vector that will contain all the columns
std::vector<double> tempVec;
std::istringstream ss(line);
//read word by word(or double by double)
while(ss >> word)
{
//std::cout<<"word:"<<word<<std::endl;
//add the word to the temporary vector
tempVec.push_back(word);
}
//now all the words from the current line has been added to the temporary vector
vec.emplace_back(tempVec);
}
}
else
{
std::cout<<"file cannot be opened"<<std::endl;
}
inFile.close();
//lets check out the elements of the 2D vector so the we can confirm if it contains all the right elements(rows and columns)
for(std::vector<double> &newvec: vec)
{
for(const double &elem: newvec)
{
std::cout<<elem<<" ";
}
std::cout<<std::endl;
}
return 0;
}
上面程序的输出可以在这里看到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.