读取文本文件并将列设为向量
Reading text file and make the column as vectors
比如我有一个文本文件名example.txt。它有两列。
12561 60295
13561 60297
13561 60298
13461 60299
15561 60300
15161 60301
15561 60302
14561 60316
12561 60317
10562 60345
15562 60346
15562 60347
15562 60348
15562 60362
19562 60363
11562 60364
12562 60365
15563 60408
15563 60409
75563 60410
65563 60411
14563 60412
13563 60413
我可以读取宏后面的文本文件:
#include <bits/stdc++.h>
using namespace std;
// driver code
int main()
{
vector<int> column1;
vector<int> column2;
fstream file;
string word, filename;
filename = "example.txt";
file.open(filename.c_str());
while (file >> word)
{
// displaying content
cout << word << endl;
}
return 0;
}
我想要做的是将第一列推回向量 column1
,将第二列推回向量 column2
。
因此向量 1 和 2 的输出为:
vector<int> column1 {12561,13561,13561,...}
vector<int> column2 {60295,60297,60298,...}
只需修改
while (file >> word)
{
// displaying content
cout << word << endl;
}
有
int n1, n2;
while (file >> n1 >> n2) {
column1.push_back(n1);
column2.push_back(n2);
}
去掉无用的变量word,可以直接读取数字,不需要先读取字符串再取数字
除此之外,我鼓励你检查你是否能够打开文件来阅读它,否则你无法区分你无法阅读的文件(可能不存在)和空文件
例如:
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
// driver code
int main()
{
ifstream file("example.txt");
if (!file)
{
cerr << "cannot read the file example.txt :"
<< strerror(errno) << endl;
return -1;
}
vector<int> column1;
vector<int> column2;
int n1, n2;
while (file >> n1 >> n2) {
column1.push_back(n1);
column2.push_back(n2);
}
// display content to check
cout << "column1:";
for (auto v : column1)
cout << ' ' << v;
cout << endl << "column2:";
for (auto v : column2)
cout << ' ' << v;
cout << endl;
return 0;
}
编译与执行:
pi@raspberrypi:/tmp $ g++ -Wall r.cc
pi@raspberrypi:/tmp $ cat example.txt
12561 60295
13561 60297
13561 60298
13461 60299
15561 60300
15161 60301
15561 60302
14561 60316
12561 60317
10562 60345
15562 60346
15562 60347
15562 60348
15562 60362
19562 60363
11562 60364
12562 60365
15563 60408
15563 60409
75563 60410
65563 60411
14563 60412
13563 60413
pi@raspberrypi:/tmp $ ./a.out
column1: 12561 13561 13561 13461 15561 15161 15561 14561 12561 10562 15562 15562 15562 15562 19562 11562 12562 15563 15563 75563 65563 14563 13563
column2: 60295 60297 60298 60299 60300 60301 60302 60316 60317 60345 60346 60347 60348 60362 60363 60364 60365 60408 60409 60410 60411 60412 60413
pi@raspberrypi:/tmp $ chmod -r example.txt
pi@raspberrypi:/tmp $ ./a.out
cannot read the file example.txt : Permission denied
pi@raspberrypi:/tmp $
比如我有一个文本文件名example.txt。它有两列。
12561 60295
13561 60297
13561 60298
13461 60299
15561 60300
15161 60301
15561 60302
14561 60316
12561 60317
10562 60345
15562 60346
15562 60347
15562 60348
15562 60362
19562 60363
11562 60364
12562 60365
15563 60408
15563 60409
75563 60410
65563 60411
14563 60412
13563 60413
我可以读取宏后面的文本文件:
#include <bits/stdc++.h>
using namespace std;
// driver code
int main()
{
vector<int> column1;
vector<int> column2;
fstream file;
string word, filename;
filename = "example.txt";
file.open(filename.c_str());
while (file >> word)
{
// displaying content
cout << word << endl;
}
return 0;
}
我想要做的是将第一列推回向量 column1
,将第二列推回向量 column2
。
因此向量 1 和 2 的输出为:
vector<int> column1 {12561,13561,13561,...}
vector<int> column2 {60295,60297,60298,...}
只需修改
while (file >> word)
{
// displaying content
cout << word << endl;
}
有
int n1, n2;
while (file >> n1 >> n2) {
column1.push_back(n1);
column2.push_back(n2);
}
去掉无用的变量word,可以直接读取数字,不需要先读取字符串再取数字
除此之外,我鼓励你检查你是否能够打开文件来阅读它,否则你无法区分你无法阅读的文件(可能不存在)和空文件
例如:
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
// driver code
int main()
{
ifstream file("example.txt");
if (!file)
{
cerr << "cannot read the file example.txt :"
<< strerror(errno) << endl;
return -1;
}
vector<int> column1;
vector<int> column2;
int n1, n2;
while (file >> n1 >> n2) {
column1.push_back(n1);
column2.push_back(n2);
}
// display content to check
cout << "column1:";
for (auto v : column1)
cout << ' ' << v;
cout << endl << "column2:";
for (auto v : column2)
cout << ' ' << v;
cout << endl;
return 0;
}
编译与执行:
pi@raspberrypi:/tmp $ g++ -Wall r.cc
pi@raspberrypi:/tmp $ cat example.txt
12561 60295
13561 60297
13561 60298
13461 60299
15561 60300
15161 60301
15561 60302
14561 60316
12561 60317
10562 60345
15562 60346
15562 60347
15562 60348
15562 60362
19562 60363
11562 60364
12562 60365
15563 60408
15563 60409
75563 60410
65563 60411
14563 60412
13563 60413
pi@raspberrypi:/tmp $ ./a.out
column1: 12561 13561 13561 13461 15561 15161 15561 14561 12561 10562 15562 15562 15562 15562 19562 11562 12562 15563 15563 75563 65563 14563 13563
column2: 60295 60297 60298 60299 60300 60301 60302 60316 60317 60345 60346 60347 60348 60362 60363 60364 60365 60408 60409 60410 60411 60412 60413
pi@raspberrypi:/tmp $ chmod -r example.txt
pi@raspberrypi:/tmp $ ./a.out
cannot read the file example.txt : Permission denied
pi@raspberrypi:/tmp $