【发布时间】:2013-09-18 18:29:42
【问题描述】:
我编写了一个程序,它一次处理一个文本文件并提取相关信息。我的程序适用于某些文本文件,而不适用于其他文本文件。在我的程序中无缝运行的文件和不通过我的程序无缝运行的文件之间没有明显的区别。
就问题文件而言:
- 程序打开文件
- 它一次读取并处理大量行,因为它应该这样做
-
但随后它到达问题行并给出错误消息:
"调试断言失败文件: f:/dd/vctools/crt_bld/self_x86/src/isctype.c 线路:56 表达式:(无符号)(c+1)
当我进入调试器模式时,问题似乎来自我下面代码中的“while(tokenScanner)”循环。我提取了正在处理的问题行的内容,并在几个问题文件中进行了比较,我发现断言失败消息在</li> 处弹出,其中最后一个正在处理的令牌是“>”。我不清楚为什么这是一个问题。原始文本文件中的这个特定标记与<li 以</li><li 的形式连续。因此,扫描仪在这个字符串的中途遇到了麻烦。
关于为什么会这样以及如何解决这个问题的任何想法?任何建议将不胜感激!
这是我的代码的相关部分:
#include <string>
#include <iostream>
#include <fstream> //to get data from files
#include "filelib.h"
#include "console.h"
#include "tokenScanner.h"
#include "vector.h"
#include "ctype.h"
#include "math.h"
using namespace std;
/*Prototype Function*/
void evaluate(string expression);
Vector<string> myVectorOfTokens; //will store the tokens
Vector<string> myFileNames;
/*Main Program*/
int main() {
/*STEP1 : Creating a vector of the list of file names
to iterate over for processing*/
ifstream infile; //declaring variable to refer to file list
string catchFile = promptUserForFile(infile, "Input file:");
string line; //corresponds to the lines in the master file containing the list files
while(getline(infile, line)){
myFileNames.add(line);
}
/* STEP 2: Iterating over the file names contained in the vector*/
int countFileOpened=0; //keeps track of number of opened files
for (int i=1; i< myFileNames.size(); i++){
myVectorOfTokens.clear(); //resetting the vector of tokens for each new file
string fileName;
string line2;
ifstream inFile;
fileName= myFileNames[i];
inFile.open(fileName.c_str()); //open file convert c_str
if (inFile){
while(getline(inFile, line2)){
evaluate(line2);
}
}
inFile.close();
countFileOpened++;
}
return 0;
}
/*Function for Extracting the Biographer Name*/
void evaluate(string line){
/*Creating a Vector of Tokens From the Text*/
TokenScanner scanner(line); //the constructor
while (scanner.hasMoreTokens()){
string token=scanner.nextToken();
myVectorOfTokens.add(token);
}
}
【问题讨论】:
-
Dieter 指出的问题很重要,而且由于在这个不可重现、不可编译的示例中,实际的标记化代码不是,因此您可能只需要得到。我建议您先解决这个问题,如果您仍有问题,请发布SSCCE,我们可以实际使用。
-
您好,感谢您的来信。我会在哪里添加文本文件来创建您建议的 SSCCEE?
-
作为您问题的更新。 不要在评论中粘贴一堵代码墙。编辑问题。我会使用 local-scope
std::ifstream进行令牌读取,但这并不重要。您对错误消息的描述表明您的问题在发布的代码中不是(超出了问题`Deiter found)。放弃标记化并在阅读时打印这些行以验证文件是否正确读取。 -
你好,请原谅我,但我是这个论坛的第一次用户,我不明白你在说什么。我尽可能详细地描述了这个问题,正如你从我的帖子中看到的那样,我已经将相关代码包含在 cmets 中。然后你提出了一个 SSCCE 帖子,根据描述需要我在我的代码旁边添加示例文本文件。我在这里错过了什么吗?
-
SSCCE 意味着您的代码发布的代码可以从字面上复制、粘贴到文本文件中、编译并显示您遇到的问题。 i> 代码应该简短、自包含、可编译(在我们的环境中;不仅仅是您的环境中)并且是您所描述问题的典范。
标签: c++ debugging text token unsigned-char