Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;打开文件时出错_C++_File Io - Fatal编程技术网

C++ C++;打开文件时出错

C++ C++;打开文件时出错,c++,file-io,C++,File Io,当我尝试在控制台应用程序中打开一个文件进行读取时,我收到以下错误消息:“homework1.exe中0x1048766d(msvcp90d.dll)处的未处理异常:0xC0000005:访问冲突写入位置0x00000000。”当我在macbook上编译和运行程序时,它工作正常,但当我在桌面上使用VS2008运行它时,它会给我这个错误 这是我的密码: int main (void){ //Open 1st file (baseproduct.dat) ifstream fin

当我尝试在控制台应用程序中打开一个文件进行读取时,我收到以下错误消息:“homework1.exe中0x1048766d(msvcp90d.dll)处的未处理异常:0xC0000005:访问冲突写入位置0x00000000。”当我在macbook上编译和运行程序时,它工作正常,但当我在桌面上使用VS2008运行它时,它会给我这个错误

这是我的密码:

int main (void){ //Open 1st file (baseproduct.dat) ifstream fin; //fin.open(filename.c_str()); fin.open("baseproduct.dat"); int tries; tries = 0; while( fin.bad() ) { if( tries >= 4 ) { cout > filename; fin.open(filename.c_str()); tries++; } SodaPop inventory[100]; //read file into array string strName; double dblPrice; int i; i = 0; fin >> strName; while( !fin.eof() ) { inventory[i].setName(strName); fin >> dblPrice; inventory[i].setPrice(dblPrice); fin >> strName; i++; } fin.close(); cout > filename; //fin.open(filename.c_str()); fin.open("soldproduct.dat"); tries = 0; while( fin.bad() ) { if( tries >= 4 ) { cout > filename; fin.open(filename.c_str()); tries++; } //read file into array i = 0; fin >> strName; while( !fin.eof() ) { cout > dblPrice; inventory[i].setPrice(dblPrice);*/ fin >> strName; i++; //1. search array for name //2. get price (what should happen with it?) //3. add # sold to quantity } fin.close(); cout 内部主(空){ //打开第一个文件(baseproduct.dat) 流鳍; //打开(filename.c_str()); 财务公开(“baseproduct.dat”); 智力测验; 尝试=0; while(fin.bad()) { 如果(尝试次数>=4) { cout>文件名; 打开(filename.c_str()); 尝试++; } SodaPop清单[100]; //将文件读入数组 字符串strName; 双dblPrice; int i; i=0; fin>>strName; 而(!fin.eof()) { 目录[i].setName(strName); fin>>dblPrice; 存货[i].设定价格(dblPrice); fin>>strName; i++; } fin.close(); cout>文件名; //打开(filename.c_str()); 财务公开(“soldproduct.dat”); 尝试=0; while(fin.bad()) { 如果(尝试次数>=4) { cout>文件名; 打开(filename.c_str()); 尝试++; } //将文件读入数组 i=0; fin>>strName; 而(!fin.eof()) { cout>dblPrice; 存货[i].设定价格(dblPrice)*/ fin>>strName; i++; //1.在数组中搜索名称 //2.获得价格(应该怎么做?) //3.添加#按数量销售 } fin.close(); cout尝试在打开时为调用添加“模式”,如:


验证您是否具有打开文件的权限:写入和读取权限。

您如何保证在读取文件时,资源清册中仍然有位置?资源清册的大小是固定的,文件本身不是……可能是资源清册索引最终指向数组本身之后,导致了访问违规


编译器和运行时可以选择是否以及如何对索引越界条件作出反应。可能,在发布模式下编译时,即使是VS2008也不会抱怨…

如果要检查文件是否打开,请不要使用
fin.bad()

while( !fin.is_open() )
{
...
}

使用VS2008时,请尝试在部分代码中放置断点


您可以按F10检查每行代码,看看它在哪里崩溃;这就是您要查看的行。

代码标记似乎没有显示所有代码,但它编译时没有错误,因此我不确定发生了什么……您可以标记引发异常的确切行吗?`cout>filename;`似乎有许多错误您发布的代码中的拼写错误,例如,荒谬的语句“cout>filename;”——这些是您的实际代码吗?以“inventory[i].setPrice(dblPrice);*/”结尾的块注释的开头在哪里?我应该澄清一下,我知道第一个文件中的唯一产品永远不会超过100个谢谢,阿拉克!您的解决方案成功了。