【问题标题】:c++ how to build a 2D matrix of strings from a .dat file? 5 columns x rowsc++ 如何从 .dat 文件构建二维字符串矩阵? 5 列 x 行
【发布时间】:2012-09-30 09:14:18
【问题描述】:

我需要读取如下所示的 .dat 文件:

Atask1 Atask2 Atask3 Atask4 Atask5
Btask1 Btask2 Btask3 Btask4 Btask5
Ctask1 Ctask2 Ctask3 Ctask4 Ctask5
Dtask1 Dtask2 Dtask3 Dtask4 Dtask5

我需要能够输出这样的信息:

cout << line(3) << endl; // required output shown below
>>Ctask1 Ctask2 Ctask3 Ctask4 Ctask5

cout << line(2)(4) << endl; // required output shown below
>>Btask4

我不知道如何读取 1 行并将其拆分为 5 个不同字符串的数组。 理想情况下,我希望将整个 .dat 文件转换为向量或列表或某种矩阵/数组结构以便于参考

有任何简单的代码或解决方案吗??

请帮忙?!?!?!? :-)

编辑:

vector<string> dutyVec[5];

dut1.open(dutyFILE);

if( !dut1.is_open() ){
    cout << "Can't open file " << dutyFILE << endl;
    exit(1);
}

    if(dut1.eof()){
    cout << "Empty file - no duties" << endl;
            exit(1);
    }

while ( !dut1.eof()){
    int count = 0;
    getline(dut1, dutyVec[count]);
    count++;
}

【问题讨论】:

  • 您知道如何从文件中读取一行文本吗?从那开始,向我们展示你到目前为止所拥有的东西。
  • 如果这是一个家庭作业,应该这样标记。
  • @TomPanning 作业标签已弃用 meta.stackexchange.com/questions/147100/…
  • @MerickOWA 感谢您的提醒。

标签: c++ arrays string vector matrix


【解决方案1】:

您的问题涉及许多问题,我将尝试一口气回答所有这些问题。所以,请原谅这篇文章的长度。

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>

int main(int argc, char argv[]){
  std::vector <std::string> v;//just a temporary string vector to store each line

  std::ifstream ifile;
  ifile.open("C://sample.txt");//this is the location of your text file (windows)

  //check to see that the file was opened correctly
  if(ifile.is_open()) { 
    //while the end of file character has not been read, do the following:
    while(!ifile.eof()) {
      std::string temp;//just a temporary string
      getline(ifile, temp);//this gets all the text up to the newline character
      v.push_back(temp);//add the line to the temporary string vector
    }
    ifile.close();//close the file
  }

  //this is the vector that will contain all the tokens that
  //can be accessed via tokens[line-number][[position-number]
  std::vector < std::vector<std::string> > tokens(v.size());//initialize it to be the size of the temporary string vector

  //iterate over the tokens vector and fill it with data
  for (int i=0; i<v.size(); i++) {
    //tokenize the string here:
    //by using an input stringstream
    //see here: http://stackoverflow.com/questions/5167625/splitting-a-c-stdstring-using-tokens-e-g
    std::istringstream f(v[i].c_str());

    std::string temp;

    while(std::getline(f, temp, ' ')) {
      tokens[i].push_back(temp);//now tokens is completely filled with all the information from the file
    }
  }
  //at this point, the tokens vector has been filled with the information
  //now you can actually use it like you wanted:
  //tokens[line-number][[position-number]

  //let's test it below:

  //let's see that the information is correct
  for (int i=0; i<tokens.size(); i++) {
    for(int j=0; j<tokens[i].size(); j++) {
      std::cout << tokens[i][j] << ' ';
    }
    std::cout << std::endl;
  }

  system("pause");//only true if you use windows. (shudder)
  return 0;
}

注意,我没有使用迭代器,这在这里会很有用。但是,我认为你可以自己尝试。

【讨论】:

  • 另请注意,标记向量是零索引(就像 C/C++ 中的所有内容一样),因此实际上,要获得第 n 行和第 n 个位置,您需要为行输入nth-1 索引,为位置输入nth-1 索引。
  • 感谢 4 抽出时间给我如此详细、完美的答案。干杯#proud linux用户
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-16
  • 2019-10-02
  • 2014-02-07
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
相关资源
最近更新 更多