【发布时间】: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