【问题标题】:Reading text file and make the column as vectors读取文本文件并将列作为向量
【发布时间】:2020-04-08 19:45:49
【问题描述】:

例如,我有一个文本文件名为 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,...} 

【问题讨论】:

    标签: c++ vector fstream


    【解决方案1】:

    只是修改

        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 $ 
    

    【讨论】:

    • 亲爱的布鲁诺,很好的答案。完全明白了。 :D
    • @zero_field 很好,你看不难;-)
    猜你喜欢
    • 1970-01-01
    • 2022-11-30
    • 2015-06-20
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    相关资源
    最近更新 更多