I keep getting the following error:
我一直收到以下错误:
Unhandled exception at 0x74BDD8A8 in FileName.exe: Microsoft C++ exception: std::out_of_range at memory location 0x004FA55C.
FileName.exe中0x74BDD8A8处的未处理异常:Microsoft C ++异常:内存位置0x004FA55C处的std :: out_of_range。
I've done some searching but I was not able to solve this problem. I did narrow it down to the fact that the out of range
error is coming from my string fdata
variable. Here is my code where error/exception occurs:
我做了一些搜索,但我无法解决这个问题。我确实将其缩小到超出范围错误来自我的字符串fdata变量的事实。这是我的代码,其中发生错误/异常:
void MyClass::MyMethod10()
{
string fdata;
char num[100];
int i = 0,k=0;
unsigned int m,j=0;
inputFile.open("sec1.txt", ios::in);
inputFile >> fdata;
while (j<fdata.length())
{
while (fdata.at(j) != '+')
{
if (fdata.at(j) != '*' && j<fdata.length())
{
num[k] = fdata.at(j);
k++;
}
else
{
num[k] = '\0';
m = atoi(num);
//cout << m << endl;
MyMethod22(m);
k = 0;
}
j++;
}
MyMethod22(43);
j++;
}
inputFile.close();
outputFile.open("sec2.txt", ios::out);
while (i<index)
{
outputFile << (char)data[i];
i++;
}
outputFile.close();
CleanBuffer();
}
The sec1.txt
file contains the following data
sec1.txt文件包含以下数据
25750*23084*57475*15982*+57475*15982*+13364*15982*26260*+48840*32397*13364*15982*57475*11371*21876*+25197*
In the while()
loop section my program is able to read the data correctly from the file. The problem/error/exception occurs at the point where my program takes in the last number from the file. I am guessing the problem is in the while()
loop, but I am not able to figure out what's wrong. All I was able to do was to narrow down the error to string fdata
being out of range after it reads the last number from the file. I was wondering if anyone can help me to solve this or suggest something which I might have missed?
在while()循环部分,我的程序能够从文件中正确读取数据。问题/错误/异常发生在我的程序从文件中获取最后一个数字的位置。我猜这个问题是在while()循环中,但我无法弄清楚是什么问题。我所能做的就是在读取文件中的最后一个数字后,将错误范围缩小到字符串fdata超出范围。我想知道是否有人可以帮助我解决这个问题或建议我可能错过的东西?
1 个解决方案
#1
6
The actual problem you have is here:
你遇到的实际问题是:
while (fdata.at(j) != '+')
{
...
j++;
}
Note that you increment j
, and try to read j
-th character before you check if j
is in range. To fix it, change it like this:
请注意,在检查j是否在范围内之前,请增加j并尝试读取第j个字符。要解决此问题,请按以下方式更改:
while (j < fdata.size() && fdata.at(j) != '+')
{
...
j++;
}
#1
6
The actual problem you have is here:
你遇到的实际问题是:
while (fdata.at(j) != '+')
{
...
j++;
}
Note that you increment j
, and try to read j
-th character before you check if j
is in range. To fix it, change it like this:
请注意,在检查j是否在范围内之前,请增加j并尝试读取第j个字符。要解决此问题,请按以下方式更改:
while (j < fdata.size() && fdata.at(j) != '+')
{
...
j++;
}