简体   繁体   中英

Visual Studio crashes when attempting to read resource .txt file

I am currently working on a project for my class and am running into a very confusing error. The code itself will launch the debugging window, whenever the file is not present. It simply prints the error message and continues with the rest of the code execution. However, when I try to invoke the readInventory() function (which was provided by my professor) into main() to read the inventory.txt file stated in f.open(), I keep getting an error. The .exe debug window opens for maybe a second, and then the error window with a green loading bar pops up saying the .exe stopped working. I have tried everything, moved the inventory.txt to different directories, and cannot get anything to work. Any kind of insight would be greatly appreciated!

I have included the code I think is needed to fix the problem, but if more of the project is needed, let me know.

readInventory()

void readInventory(inventoryItem inv[], int & numberOfInvItems, int & lastOrderNum) {
ifstream f;

// open the inventory file
f.open("inventory.txt");
if (f.fail()) {
    cout << "readFile:: error opening inventory.txt\n";
    numberOfInvItems = READ_ERROR;
    return;
}

// read number of items from first line
f >> numberOfInvItems >> lastOrderNum;

// for each item, read the data for the item
for (int i = 0; i < numberOfInvItems; i++) {
    f >> inv[i].prodCode >> inv[i].price;
    f.ignore(); // finished reading integer, getline() on string is next
    getline(f, inv[i].description);
}
f.close();
}

main()

int main(int numberOfInvItems, inventoryItem inv[], int lastOrderNum, char &   option, bool orderItem2 , int lastOrderNumber, basket Order[], inventoryItem item[])
{
int b = -1;
const int MAX_INV_ITEMS = 10, MAX_BASKETS = 7;
bool orderNumber2 = false;
inventoryItem Inventory[MAX_INV_ITEMS];
basket Orders[MAX_BASKETS];


readInventory(inv, numberOfInvItems,lastOrderNum);

for (int m = 0; m < numberOfInvItems; m++) {
    //Inventory[m].prodNum = m;
    Inventory[m].prodCode = inv[m].prodCode;
    Inventory[m].description = inv[m].description;
    Inventory[m].price = inv[m].price;
}

lastOrderNumber = lastOrderNum;

while (option != 'X') {
    getmainOption(option, item, numberOfInvItems, Inventory, orderItem2, b, MAX_BASKETS, lastOrderNumber, Order);
}

system("pause");
return 0;

}

At a glance, here are some tips. Your code needs many more safety checks & small fixes:

Your main function signature seems to be sus:

int main(int numberOfInvItems, inventoryItem inv[], ...... .....) { }

should really be

int main(int argc, char *argv[]) { }

Note: if this isn't your ACTUAL APPLICATION MAIN function, then do NOT call it main.

Secondly, your code readInventory method should really return a bool if it was successful or not, and only return the correct values via the arguments: ie:

bool readInventory(inventoryItem inv[], int& numberOfInvItems, int& lastOrderNum)
{
    // Could not open
    if(f.fail())
        return false; // Dont set NumberOfInvItems to READ_ERROR? WHAT DOES THAT EVEN MEAN?

    // ...

    // Successful
    return true;
}

and in your main function

int main(...)
{
    int numberOfInvItems = 0;
    int lastOrderNum     = 0;

    // Ensure it was successful via the return type
    if(readInventory(inv, numberOfInvItems,lastOrderNum))
    {
        // ALL VALUES WEOULD BE VALID HERE
        for(int i = 0; i < numberOfInvItems; ++i) {
            // ...
        }
    }

}

Thirdly, my opinion is to avoid using C arrays. I'd suggest swapping them to std::vectors, or at least, a std::array to help reduce possible bugs.

Finally, in your for loop:

for (int m = 0; m < numberOfInvItems; m++) {
    //Inventory[m].prodNum = m;
    Inventory[m].prodCode = inv[m].prodCode;
    Inventory[m].description = inv[m].description;
    Inventory[m].price = inv[m].price;
}

Have you verified, or FORCED both Inventory[] and inv[] to be the same size? what if one size is larger than the other - it WILL crash. Either iterate to the smallest of either one, or dont iterate at all.

You should add debugging (std::couts or printf's) after each line to see where it crashes specifically. Debug Debug Debug. You can then determine what line / function / expression caused the problem. If it crashes and no debugging was printed at all, then its probably due to your dodgy main function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM