将基于文本的列数据存储到数组 C++ 中
Store text based columns data into array C++
我正在尝试将文本文件中的数据存储到数组中,以便以后使用。问题是当每个数据都有一个表头时,如何在文本中间跳过一行或单独取?
#include <iostream>
#include <fstream>
main() {
double tab[100][6] = {0};
int i =0, j;
std::string name = "test.txt";
// std::cout << "Enter filename: ";
// std::cin >> name;
std::fstream file;
std::string word;
file.open(name.c_str());
std::getline(file,word); // skip the first line
while(file >> word) { //take word and print
std::cout << word << std::endl;
for(int j=0; j<=5; j++){
tab[i][j] = stoi(word);
}
i++;
}
file.close();
// displaying
for(int j = 0; j<= 40;j++){
std::cout << tab[i][0] << "\t" << tab[i][1] << "\t" << tab[i][2] << "\t" << tab[i][3] << "\t" << tab[i][4] << "\t" << tab[i][5];
}
}
test.txt 文件
18407022 2018-07-05 00:04:02 MHAM EIDW 42 S1REB RYR5GW 3726 JNEIE 837B Datum RYR IFR Undefined 1 1 2018-07-05 00:15:38 2018-07-05 00:22:56 111 extended
0.0 113416.9 479798.5 -0.2 3.6 0.0
4.0 113395.2 479785.0 1.2 9.3 25.5
8.0 113352.2 479758.7 1.2 16.0 75.9
12.0 113284.9 479717.4 0.5 23.6 154.9
16.0 113189.9 479659.1 -0.5 32.2 266.3
20.0 113064.3 479582.1 -0.9 41.7 413.7
24.0 112904.9 479483.9 -0.3 52.1 600.9
18407022 2018-07-05 00:12:14 MHAM EIDW 42 S1REB RYR5GW 3726 JNEIE 837B Datum RYR IFR Undefined 1 1 2018-07-05 00:30:38 2018-07-05 00:42:56 111 extended
0.0 145431.6 480046.3 4533.3 180.4 0.0
4.0 144747.1 480268.2 4493.4 179.5 719.5
8.0 144059.0 480468.7 4452.0 179.0 1436.2
12.0 143368.6 480655.3 4409.8 178.7 2151.4
16.0 142677.0 480835.6 4367.6 178.6 2866.1
20.0 141985.8 481017.2 4326.1 178.8 3580.9
24.0 141295.9 481207.3 4286.1 179.0 4296.4
也许你先看完整行,有没有字母(a-zA-Z)?!
#include <regex>
...
std::regex e("a-zA-Z");
while(file >> word) { //take word and print
if ( std::regex_match ( word, e))
continue;
...
您可以尝试将文本文件中的每个 header 标记为注释,方法是在每个 header.
的开头放置一个 #
现在像这样阅读文本文件:
#include<sstream>
...
...
std::ifstream file {"test.txt"};
std::string line;
std::istringstream iss;
double tab[100][6];
int i=0, j;
...
while (getline(file, line))
{
if (!(line[0]=='#'))
{
iss.str(line);
for (j=0; j<6; ++j)
iss >> tab[i][j];
iss.clear();
++i;
}
}
file.close();
我正在尝试将文本文件中的数据存储到数组中,以便以后使用。问题是当每个数据都有一个表头时,如何在文本中间跳过一行或单独取?
#include <iostream>
#include <fstream>
main() {
double tab[100][6] = {0};
int i =0, j;
std::string name = "test.txt";
// std::cout << "Enter filename: ";
// std::cin >> name;
std::fstream file;
std::string word;
file.open(name.c_str());
std::getline(file,word); // skip the first line
while(file >> word) { //take word and print
std::cout << word << std::endl;
for(int j=0; j<=5; j++){
tab[i][j] = stoi(word);
}
i++;
}
file.close();
// displaying
for(int j = 0; j<= 40;j++){
std::cout << tab[i][0] << "\t" << tab[i][1] << "\t" << tab[i][2] << "\t" << tab[i][3] << "\t" << tab[i][4] << "\t" << tab[i][5];
}
}
test.txt 文件
18407022 2018-07-05 00:04:02 MHAM EIDW 42 S1REB RYR5GW 3726 JNEIE 837B Datum RYR IFR Undefined 1 1 2018-07-05 00:15:38 2018-07-05 00:22:56 111 extended
0.0 113416.9 479798.5 -0.2 3.6 0.0
4.0 113395.2 479785.0 1.2 9.3 25.5
8.0 113352.2 479758.7 1.2 16.0 75.9
12.0 113284.9 479717.4 0.5 23.6 154.9
16.0 113189.9 479659.1 -0.5 32.2 266.3
20.0 113064.3 479582.1 -0.9 41.7 413.7
24.0 112904.9 479483.9 -0.3 52.1 600.9
18407022 2018-07-05 00:12:14 MHAM EIDW 42 S1REB RYR5GW 3726 JNEIE 837B Datum RYR IFR Undefined 1 1 2018-07-05 00:30:38 2018-07-05 00:42:56 111 extended
0.0 145431.6 480046.3 4533.3 180.4 0.0
4.0 144747.1 480268.2 4493.4 179.5 719.5
8.0 144059.0 480468.7 4452.0 179.0 1436.2
12.0 143368.6 480655.3 4409.8 178.7 2151.4
16.0 142677.0 480835.6 4367.6 178.6 2866.1
20.0 141985.8 481017.2 4326.1 178.8 3580.9
24.0 141295.9 481207.3 4286.1 179.0 4296.4
也许你先看完整行,有没有字母(a-zA-Z)?!
#include <regex>
...
std::regex e("a-zA-Z");
while(file >> word) { //take word and print
if ( std::regex_match ( word, e))
continue;
...
您可以尝试将文本文件中的每个 header 标记为注释,方法是在每个 header.
的开头放置一个#
现在像这样阅读文本文件:
#include<sstream>
...
...
std::ifstream file {"test.txt"};
std::string line;
std::istringstream iss;
double tab[100][6];
int i=0, j;
...
while (getline(file, line))
{
if (!(line[0]=='#'))
{
iss.str(line);
for (j=0; j<6; ++j)
iss >> tab[i][j];
iss.clear();
++i;
}
}
file.close();