Open File Failed

ifstream open file failed

Page 1 of 1

6 Replies - 8389 Views - Last Post: 25 April 2010 - 09:05 PM Rate Topic: -----

#1 JennaPeterson88   User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 39
  • Joined: 17-March 10

Open File Failed

Posted 24 April 2010 - 09:58 PM

	string bookTitle[size];
	ifstream ins;
	ins.open("inventory.txt");
	if (ins.fail())
	{
		cout << "Error: Could not load inventory!" << endl << endl;
		return 1;
	}


(This is just part of my code, obviously, near the top of the int main() function.
Yes, I have included fstream
Yes, inventory.txt exists and is included in the project, in the correct spot. (I'm using Visual C++ 2008 Express Edition)

I've opened text files exactly the same way in other programs and it works.

Any ideas as to why it's not opening now?
ins.fail() seems to be true, because the program tells me Error: Could not load inventory! and closes.
Without checking that the file opened, the program runs as it should, so there aren't any problems with the rest of the code.



Entire .cpp so far-
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "myheader.h"

using namespace std;

void checkdate(string date); // Not included in header file because Studio won't accept "string" in stubs...

const int size = 20; // The maximum size for all arrays (maximum uniqye inventory spaces)

int main()
{
	string bookTitle[size];
	ifstream ins;
	ins.open("inventory.txt");
	if (ins.fail())
	{
		cout << "Error: Could not load inventory!" << endl << endl;
		return 1;
	}

	string isbn[size];
	string author[size];
	string publisher[size];
	string dateAdded[size];
	string qtyOnHand[size];
	string wholesale[size];
	string retail[size];
	int numBooks = 0;
	string date; // YYYY/MM/DD format
	

	int choice = 0;

    do {
		checkdate(date);
        cout << setw(40) << "Serendipity Booksellers" << endl;
        cout << setw(33) << "Main Menu" << endl << endl;
        cout << "1. Cashier Mode" << endl << "2. Inventory Database Module" << endl << "3. Report Module" << endl << "4. Exit" << endl << endl;
        cout << "Enter Your Choice: ";
        cin >> choice;
        cout << endl;

        if (choice < 5 && choice > 0)
        {
            switch (choice)
            {
                case 1: cashier(); break;
                case 2: invmenu(); break;
                case 3: reports(); break;
                case 4: cout << "Goodbye!" << endl << endl; break;
            }
        }
        else
        {
        cout << "Please enter a value 1-4" << endl << endl;
        }

    } while (choice != 4);


}
void lookUpBook()
{
    // This function will eventually allow the user to look up a book in the inventory database
    cout << "You selected Look Up Book" << endl << endl;
}

void addBook()
{
    // this function will eventually allow the user to add a book to the inventory database
    cout << "You selected Add Book" << endl << endl;
}

void editBook()
{
    // This function will eventually allow the user to edit a book in the inventory database
    cout << "You selected Edit Book" << endl << endl;
}

void deleteBook()
{
    // This function will eventually allow the user to delete a book from the inventory database
    cout << "You selected Delete Book" << endl << endl;
}

void repListing()
{
    // This function will eventually give a complete listing from the inventory database
    cout << "You selected Inventory Listing" << endl << endl;
}

void repWholesale()
{
    // This function will eventually display to the user the wholesale value of the inventory
    cout << "You selected Inventory Wholesale Value" << endl << endl;
}

void repRetail()
{
    // This function will eventually display to the user the retail value of the inventory
    cout << "You selected Inventory Retail Value" << endl << endl;
}

void repQty()
{
    // This function will eventually list the inventory by quantity in stock
    cout << "You selected Listing By Quantity" << endl << endl;
}

void repCost()
{
    // This function will eventually list the inventory by cost
    cout << "You selected Listing By Cost" << endl << endl;
}

void repAge()
{
    // This function will eventually list the inventory by age of listings
    cout << "You selected Listing By Age" << endl << endl;
}


void cashier()
{
    char choice;

    do
    {
    string date, title, book_isbn;
    int quantity_books;
    double price, subtotal, taxcharged, total;
    cin.clear();

    cout << setw(40) << "Serendipity Booksellers" << endl;
    cout << setw(34) << "Cashier Mode" << endl << endl;

    cout << "Enter Date (YYYY/MM/DD): ";
    cin >> date; // In order for the receipt to print nicely, the date must be entered in the exmaple format
    cout << "Quantity: ";
    cin >> quantity_books;
    cout << "ISBN: ";
    cin >> book_isbn; // ISBN number is assumed to be entered in the 0-000-00000-0 format
    cout << "Title: ";
    cin.ignore(100,'\n');
    getline(cin,title); // This little piece of code allows the user to enter a string with spaces for the title that will not truncate at the first space
    cout << "Price: $";
    cin >> price; // price is entered as a number with or without a decimal value, it doesn't matter.


    subtotal = price * quantity_books; // obtain the transaction subtotal
    taxcharged = subtotal * 0.06; // 0.06 is the tax rate
    total = subtotal + taxcharged; // obtain the total amount of the transaction

    int SBTP; // SBTP = Spacing between the title and the price
    int n = title.length();
    SBTP = 31 - n;
   

    cout << endl << endl << endl << endl << setw(30) << "Serendipity Booksellers" << endl << endl << "Date: " << date << endl << endl;
    cout << setw(3) << "Qty" << setw(5) << "ISBN" << setw(10) << " " << "Title" << setw(26) << "Price" << setw(7) << "Total" << endl;
    cout << "_________________________________________________________________" << endl;
    cout << setw(3) << quantity_books << setw(14) << book_isbn << " " << title << setw(SBTP) << fixed << setprecision(2) << price << setw(7) << setprecision(2) << subtotal << endl;
    cout << endl;
    cout << setw(25) << "Subtotal" << setw(31) << subtotal << endl << setw(20) << "Tax" << setw(36) << taxcharged << endl << setw(22) << "Total" << setw(34) << total << endl << endl;
    cout << endl << "Start another transaction? (y/n): ";
    cin >> choice;
    cin.clear();
    cout << endl << endl;
   
    } while (choice != 'n');

    cout << "Thank you!" << endl << endl << "Returning to main menu, please wait..." << endl << endl;

}


void invmenu()
{
    int choice;
    choice = 0;
    do
    {
    cout << setw(40) << "Serendipity Booksellers" << endl;
    cout << setw(38) << "Inventory Database" << endl << endl;
    cout << "1. Look Up a Book" << endl << "2. Add a Book" << endl << "3. Edit a Book's Record" << endl << "4. Delete a Book" << endl << "5. Return to the Main Menu" << endl << endl;
    cout << "Enter Your Choice: ";
    cin >> choice;
    cout << endl;

    if (choice < 1 || choice > 5)
    {
        cout << "Please enter a number in the range 1-5" << endl << endl;
    }
    else
    {
        cout << endl;
    }
    } while (choice < 1 || choice > 5);

    switch (choice)
    {
    case 1: lookUpBook(); break;
    case 2: addBook(); break;
    case 3: editBook(); break;
    case 4: deleteBook(); break;
    case 5: cout << "Returning to main menu..." << endl << endl; break;
    }

}

void bookinfo (int)
{
}

void reports()
{

    int choice;
    choice = 0;
   
    do
    {
    cout << setw(40) << "Serendipity Booksellers" << endl;
    cout << setw(32) << "Reports" << endl << endl;
    cout << "1. Inventory Listing" << endl << "2. Inventory Wholesale Value" << endl << "3. Inventory Retail Value" << endl << "4. Listing by Quantity" << endl << "5. Listing by Cost" << endl << "6. Listing by Age" << endl << "7. Return to the Main Menu" << endl << endl;

    cout << "Enter Your Choice: ";
    cin >> choice;
    cout << endl;

    if (choice < 1 || choice > 7)
    {
        cout << "Please enter a number in the range 1-7" << endl << endl;
    }
    else
    {
        cout << endl;
    }
    } while (choice < 1 || choice > 7);

    switch (choice)
    {
    case 1: repListing(); break;
    case 2: repWholesale(); break;
    case 3: repRetail(); break;
    case 4: repQty(); break;
    case 5: repCost(); break;
    case 6: repAge(); break;
    case 7: cout << "Returning to main menu..." << endl << endl; break;
    }

}
void checkdate(string date)
{
	if (date == "")
	{
		cout << "Welcome back! What is the date today? (YYY/MM/DD)" << endl;
		cin >> date;
		cout << endl;
	}
}

This post has been edited by JennaPeterson88: 24 April 2010 - 10:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Open File Failed

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Open File Failed

Posted 24 April 2010 - 10:59 PM

Quoting ifstream::open

Quote

If the object already has a file associated (open), the function fails.


So it's saying ifstream ins; is already associated with a file.

This post has been edited by n8wxs: 24 April 2010 - 11:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Open File Failed

Posted 25 April 2010 - 01:15 AM

this might sound dumb, but go to your control panel and go into folder options and check display file extensions. then take a look at the file again and check its extension type.
Was This Post Helpful? 0
  • +
  • -

#4 JennaPeterson88   User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 39
  • Joined: 17-March 10

Re: Open File Failed

Posted 25 April 2010 - 05:37 PM

n8wxs - It should be associated with any file, because that's the first line in which "ins" appears in the code. In fact, I had it called other things that made more senes, but went back to ins when I copy & pasted this code section from a previous project that worked and changed the file name. What would it be associated with?

ImaSexy - not a stupid suggestion, actually, sounds like something I could have easily missed, but I've tripled checked and the file extension is correct.

n8wxs - It should be associated with any file, because that's the first line in which "ins" appears in the code. In fact, I had it called other things that made more senes, but went back to ins when I copy & pasted this code section from a previous project that worked and changed the file name. What would it be associated with?

ImaSexy - not a stupid suggestion, actually, sounds like something I could have easily missed, but I've tripled checked and the file extension is correct.

n8wxs - It should be associated with any file, because that's the first line in which "ins" appears in the code. In fact, I had it called other things that made more senes, but went back to ins when I copy & pasted this code section from a previous project that worked and changed the file name. What would it be associated with?

ImaSexy - not a stupid suggestion, actually, sounds like something I could have easily missed, but I've tripled checked and the file extension is correct.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Open File Failed

Posted 25 April 2010 - 05:47 PM

View PostJennaPeterson88, on 25 April 2010 - 06:37 PM, said:

n8wxs - It should be associated with any file, because that's the first line in which "ins" appears in the code. In fact, I had it called other things that made more senes, but went back to ins when I copy & pasted this code section from a previous project that worked and changed the file name. What would it be associated with?

ImaSexy - not a stupid suggestion, actually, sounds like something I could have easily missed, but I've tripled checked and the file extension is correct.

n8wxs - It should be associated with any file, because that's the first line in which "ins" appears in the code. In fact, I had it called other things that made more senes, but went back to ins when I copy & pasted this code section from a previous project that worked and changed the file name. What would it be associated with?

ImaSexy - not a stupid suggestion, actually, sounds like something I could have easily missed, but I've tripled checked and the file extension is correct.

n8wxs - It should be associated with any file, because that's the first line in which "ins" appears in the code. In fact, I had it called other things that made more senes, but went back to ins when I copy & pasted this code section from a previous project that worked and changed the file name. What would it be associated with?

ImaSexy - not a stupid suggestion, actually, sounds like something I could have easily missed, but I've tripled checked and the file extension is correct.


Three, three, three posts in one! :D

If you give the full path to the inventory.txt file, does it work?
Was This Post Helpful? 0
  • +
  • -

#6 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Open File Failed

Posted 25 April 2010 - 05:50 PM

Quote

Yes, inventory.txt exists and is included in the project, in the correct spot.
Sorry, but I can't take you on your word. Given that your program fails to work, I am not going to assume you did things correctly. Instead, why don't you describe where you placed this inventory.txt file?

When you run the program directly, it checks relative to the program directory. In other words, the text file needs to be where the program is. However, when you run from the IDE, the text file needs to be where the project file is.
Was This Post Helpful? 0
  • +
  • -

#7 JennaPeterson88   User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 39
  • Joined: 17-March 10

Re: Open File Failed

Posted 25 April 2010 - 09:05 PM

Sorry about the multiple posts in one, my internet kept reconnecting.

Anyway, solved it. Not entirely sure what I did differently this time to solve it but hey, that's what happens when you work too many early shifts and then try to program, right? :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1