Exception 尝试使用ifstream时引发异常
我试图将文本文件中的行放入Exception 尝试使用ifstream时引发异常,exception,visual-c++,ifstream,c++17,Exception,Visual C++,Ifstream,C++17,我试图将文本文件中的行放入字符串的数组中,但我遇到了一个异常,坦白地说,我不知道该怎么做 下面是处理该文件的代码: ifstream names("Vehicle_names.txt"); string vehnames[54]; if (names.bad()) { return 1; } while (names) { names >> vehnames[i++]; } i = 0;
字符串的数组中,但我遇到了一个异常,坦白地说,我不知道该怎么做
下面是处理该文件的代码:
ifstream names("Vehicle_names.txt");
string vehnames[54];
if (names.bad())
{
return 1;
}
while (names)
{
names >> vehnames[i++];
}
i = 0;
while (i < 54)
{
cout << endl << vehnames[i];
i++;
}
错误是这样的:
Excpetion thrown: write access violation
_Left was 0xCCCCCCCC.
如果你能帮我做这件事,我会非常感激的,我想做点什么
如果有帮助的话,我正在使用Visual Studio 2017进行写作。首先,看起来您正在使用I而没有声明I。接下来,确保i没有指向什么都找不到的地方。也许试试这样的
ifstream names("Vehicle_names.txt");
int i = 0;
string vehnames[1028]; //ensure there is enough memory allocated to store txt file in string array
if (names.bad())
return 1;
while (names.good())
names >> vehnames[i++];
int j = 0;
while(j<i)
cout << vehnames[j++] << endl;
return 0;
ifstream名称(“Vehicle_names.txt”);
int i=0;
字符串vehnames[1028]//确保分配了足够的内存以在字符串数组中存储txt文件
if(name.bad())
返回1;
while(names.good())
名称>>vehnames[i++];
int j=0;
虽然(JET),熟悉“写访问冲突”几乎是肯定的,因为您在代码< > < <代码> >之前已经将 I/COD>作为数组索引,输入到 > Vields>代码中,因此您无法预测什么<>代码> i>代码>的值。它可能是任何东西,并且C++在运行时不检查您是唯一的。使用普通数组范围内的索引。相反,当操作系统检测到您试图访问不应该访问的内存时,您可能会从操作系统本身获得异常。
ifstream names("Vehicle_names.txt");
int i = 0;
string vehnames[1028]; //ensure there is enough memory allocated to store txt file in string array
if (names.bad())
return 1;
while (names.good())
names >> vehnames[i++];
int j = 0;
while(j<i)
cout << vehnames[j++] << endl;
return 0;