问题描述
我编写了一个程序,一次处理文本文件并提取相关信息。我的程序适用于一些文本文件,而不是其他文件。通过我的程序无法运行的文件和没有程序的文件之间没有明显区别。对于有问题的文件:
- 程序打开文件
- 它会一次读入并处理一行中的一个很好的块,因为它应该
-
但是,它达到了一个问题行并给出错误消息:
调试断言失败的文件:
f:/dd/vctools/crt_bld/self_x86/src/isctype.c
行:56
表达式:(无符号)(c + 1)< = 256
< / li> 其中正在处理的最后一个令牌是>。我不清楚为什么这是一个问题。原始文本文件中的这个特定令牌与
< / li>< li
。因此扫描仪在这个字符串中遇到麻烦。
任何关于为什么这样的想法,以及我如何解决这个问题?任何建议将不胜感激!
以下是我代码的相关部分:
code> #include< string>
#include< iostream>
#include< fstream> //从文件获取数据
#includefilelib.h
#includeconsole.h
#includetokenScanner.h
#includevector.h
#includectype.h
#includemath.h
using namespace std;
/ *原型函数* /
void evaluate(string expression);
向量< string> myVectorOfTokens; //将存储令牌
向量< string> myFileNames;
/ *主程序* /
int main(){
/ * STEP1:创建文件名列表
的向量进行迭代结束处理* /
ifstream infile; //声明变量引用文件列表
string catchFile = promptUserForFile(infile,Input file:);
字符串; //对应于包含列表文件的主文件中的行
while(getline(infile,line)){
myFileNames.add(line);
}
/ *步骤2:迭代向量中包含的文件名* /
int countFileOpened = 0; //跟踪已打开文件的数量
for(int i = 1; i< myFileNames.size(); i ++){
myVectorOfTokens.clear(); //重新设置每个新文件的令牌向量
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;
}
/ *提取传记作者名称的功能* /
void evaluate(string line){
/ *从文本创建令牌向量* /
TokenScanner扫描器(行); //构造函数
while(scanner.hasMoreTokens()){
string token = scanner.nextToken();
myVectorOfTokens.add(token);
}
}
只是错误(几乎任何情况下)
while(getline(inFile,line2))
/ pre>
evaluate(line2);
更好
I have written a program that processes text files one at a time and extract relevant information. My program works well with some of the text files and not others. There is no obvious difference between the files that run seamlessly through my program and those that don't.
As far as the problematic files are concerned:
- the program opens the file
- it reads in and processes a good chunk of the lines one at a time as it should
But then it reaches a problem line and gives the error message:
"Debug Assertion Failed File: f:/dd/vctools/crt_bld/self_x86/src/isctype.c Line: 56 Expression: (unsigned)(c+1) <= 256"
When I enter the debugger mode the problem seems to arise from the "while(tokenScanner)" loop in my code below. I pulled up the content of the problem line being processed and compared that across a couple of problem files and I found that the Assertion Failure message pops up at
</li>
where the last token being processed is ">". It's not clear to me why this is a problem. This particular token in the original text file is contiguous with<li
in the form</li><li
. Therefore the scanner is having trouble half way throught this string. Any thoughts on why this is and how I can fix this? Any advice would be much appreciated!Here is the relevant portion of my code:
#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); } }
解决方案is just wrong (in almost any case)
while(getline(inFile, line2)) evaluate(line2);
is better
这篇关于调试断言失败文件,tokenScanner和文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!