Code::Blocks Forums

User forums => Help => Topic started by: fuzzymonkey on February 25, 2010, 05:08:23 am

Title: Problem with reading in files on mac.
Post by: fuzzymonkey on February 25, 2010, 05:08:23 am
I made a small text file that used to read in a few values into my program. However, when I run the program (without any compiler errors) The file does not get opened. It is saved in the same folder as the program so there should be no problem. Any clue why it is happening?

Oh, and just to show I am not having some error in the code, here is the relevant lines of code:

#include <iostream>
#include <fstream>

#include <cstdlib>

ifstream inventoryFile;
    inventoryFile.open("inventory.in");
    if (!inventoryFile) {
        cerr << "File inventory.in could not be opened" << endl;
        return 1;
    }


    int num_receipts;
    int
        salesman1_total = 0,
        salesman2_total = 0;
    int price, cost;
    int initial_stock_on_hand;

    inventoryFile >> initial_stock_on_hand;
    inventoryFile >> price;
    inventoryFile >> cost;

    inventoryFile.close();
Title: Re: Problem with reading in files on mac.
Post by: Jenna on February 25, 2010, 06:35:59 am
If you run your program from inside C::B, the program's execution working dir is set to the projects base dir by default.

You can change this to the dir where the executable is build.

Right-click on your project in the managemnet pane, chose "Properties -> Build targets -> [the Target you want to change]" and change "Execution working dir" to fit your needs.
Title: Re: Problem with reading in files on mac.
Post by: fuzzymonkey on February 25, 2010, 06:44:08 am
This isn't a project, just a file. How would I do it without a project? Or would I have to make it into a project?

Thanks.
Title: Re: Problem with reading in files on mac.
Post by: Jenna on February 25, 2010, 06:55:09 am
In this case it should work, if the file is in the same directory as the executable.

Try to run your program from a console and see if it works then, if not, it's ndefinitely not a C::B problem.

And make sure the access-rights for your text-file are set correctly.
Title: Re: Problem with reading in files on mac.
Post by: fuzzymonkey on March 25, 2010, 06:13:30 pm
How do I run it form a console?
Title: Re: Problem with reading in files on mac.
Post by: Smirftsch on March 27, 2010, 03:29:26 pm
open a terminal, change it the directory with "cd /wherever/your/app/is" and execute it with "./MyApp"
if it can't be executed then you need to chmod it probably to something executable, like "chmod 777 MyApp" and then try "./MyApp" again