【发布时间】:2013-05-17 08:38:07
【问题描述】:
我的构造函数的目标是:
打开一个文件 读入特定字符串之间存在的所有内容(“%%%%%”) 将每个读取的行放在一个变量(历史)中 将最终变量添加到 char (_stories) 类型的双指针 关闭文件。
但是,当我使用 strcat 时,程序崩溃了。但我不明白为什么,我尝试了好几个小时都没有结果。 :/
这里是构造函数代码:
Texthandler::Texthandler(string fileName, int number)
: _fileName(fileName), _number(number)
{
char* history = new char[50];
_stories = new char*[_number + 1]; // rows
for (int j = 0; j < _number + 1; j++)
{
_stories[j] = new char [50];
}
_readBuf = new char[10000];
ifstream file;
int controlIndex = 0, whileIndex = 0, charCounter = 0;
_storieIndex = 0;
file.open("Historier.txt"); // filename
while (file.getline(_readBuf, 10000))
{
// The "%%%%%" shouldnt be added to my variables
if (strcmp(_readBuf, "%%%%%") == 0)
{
controlIndex++;
if (controlIndex < 2)
{
continue;
}
}
if (controlIndex == 1)
{
// Concatenate every line (_readBuf) to a complete history
strcat(history, _readBuf);
whileIndex++;
}
if (controlIndex == 2)
{
strcpy(_stories[_storieIndex], history);
_storieIndex++;
controlIndex = 1;
whileIndex = 0;
// Reset history variable
history = new char[50];
}
}
file.close();
}
我也尝试过没有结果的字符串流..
编辑:忘记发布错误消息: “Step3_1.exe 中 0x6b6dd2e9 (msvcr100d.dll) 处的未处理异常:0xC00000005:访问冲突写入位置 0c20202d20。” 然后打开一个名为“strcat.asm”的文件..
最好的问候 罗伯特
【问题讨论】:
标签: c++ visual-c++