【问题标题】:parsing a text file with first line all 1's and second line all 2's解析一个文本文件,第一行全为 1,第二行全为 2
【发布时间】:2013-08-16 10:19:46
【问题描述】:

我创建了一个程序,该程序将逐行读取文本文件,并将每行中的各个单词分开(以空格分隔)。 现在我希望能够编辑代码,以便第一行的所有标记都是 1,第二行的所有标记都是 2 如果有人可以帮助我,请 下面是我的代码:

#include <iostream>
using std::cout;
using std::endl;

#include <fstream>
using std::ifstream;
#include <cmath>
#include <string>

const int MAX_CHARS_PER_LINE = 512;
const int MAX_TOKENS_PER_LINE = 20;
const char* const DELIMITER = " ";
using namespace std;
int main()
{
    string filename;
  // create a file-reading object
 /*std::ifstream file1("file1.txt", ios_base::app);
std::ifstream file2("file2.txt");

std::ofstream combinedfile("combinedfile.txt");
combinedfile << file1.rdbuf() << file2.rdbuf();*/

  ifstream fin;
  //enter in file name combinedfile.txt
  cout <<"please enter file name (including .txt)";
  cin >> filename ;
  fin.open(filename); // open a file
  if (!fin.good()) 
    return 1; // exit if file not found

  // read each line of the file
  while (!fin.eof())
  {
    // read an entire line into memory
    char buf[MAX_CHARS_PER_LINE];
    fin.getline(buf, MAX_CHARS_PER_LINE);

    // parse the line into blank-delimited tokens
    int n = 0; // a for-loop index

    // array to store memory addresses of the tokens in buf
    const char* token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0

    // parse the line
    token[0] = strtok(buf, DELIMITER); // first token
    if (token[0]) // zero if line is blank
    {
      for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
      {
        token[n] = strtok(0, DELIMITER); // subsequent tokens
        if (!token[n]) break; // no more tokens
      }
    }

    // process (print) the tokens
    for (int i = 0; i < n; i++) // n = #of tokens
      cout << "Token[" << i << "] = " << token[i] << endl;
    cout << endl;
  }
  system("pause");
  return 0;
}

所以输出应该是这样的:

Token[1] = This
Token[1] = course
Token[1] = provides
Token[1] = detailed
Token[1] = coverage
Token[1] = of
Token[1] = the
Token[1] = concepts
Token[1] = and
Token[1] = syntax

Token[2] = Coverage
Token[2] = includes
Token[2] = inheritance,
Token[2] = overloaded
Token[2] = operators,
Token[2] = overloaded
Token[2]= default
Token[2] = operators,

【问题讨论】:

  • “1”和“2”是什么意思?您能否提供更多信息,您究竟想做什么?
  • 所以基本上你知道如何使用令牌他们可以数到你设置的任何数字。我希望它只为我的文本文件的第一行输出 Token[1],每个单词都有 Token[1] 并且第二行只使用 Token[2] 并且该行中的每个单词都有 Token[2 ]
  • @user2653471 你还是没有任何意义,你介意在你的帖子中举个例子吗?
  • 我不明白这里的单词和标记的区别。您是否只想为第一个字输出第二个( token[1] ),为行输出第三个( token[2] )字?
  • 我在帖子里放了一个例子

标签: c++ parsing text-files token


【解决方案1】:

如果您只想将单词分成两个容器,其中第一个包含第一行中的单词,第二个包含第二行中的单词,您可以使用向量来存储它们并使用字符串流从一行中提取单词文本:

#include <sstream>
#include <string>
#include<vector>
#include<fstream>
using namespace std;

int main()
{
    ifstream infile("test.txt");
    string line;
    string word;
    vector< vector<string> >  tokens(2);
    for (int ix = 0; ix < 2; ++ix)
    {    
        getline(infile, line);
        istringstream iss(line);
        while(iss >> word)
            tokens[ix].push_back(word);
    }   
}

这里,tokens[0] 是一个向量,包含来自第一行的单词,tokens[1] 包含来自第二行的单词。

【讨论】:

  • +1,但是,while(getline(infile, line)) 应该被用来代替 for 循环
【解决方案2】:

您可以将向量的向量创建为

vector<vector<string>> VEC;

并继续在第一行向VEC[0] 添加单词,然后在遇到换行符以指向VEC[1] 时递增计数器,依此类推。

【讨论】:

  • 是否有任何替代方案我只是想看看是否有不止一种方法
  • @user2653471 方式可能更多,但这可能是最有效的方式
猜你喜欢
  • 2020-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-19
  • 1970-01-01
  • 2021-08-10
  • 2014-07-06
  • 1970-01-01
相关资源
最近更新 更多