Author Topic: Problem with reading in files on mac.  (Read 10334 times)

Offline fuzzymonkey

  • Multiple posting newcomer
  • *
  • Posts: 11
Problem with reading in files on mac.
« 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();
« Last Edit: February 25, 2010, 05:11:21 am by fuzzymonkey »

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: Problem with reading in files on mac.
« Reply #1 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.

Offline fuzzymonkey

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: Problem with reading in files on mac.
« Reply #2 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.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: Problem with reading in files on mac.
« Reply #3 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.

Offline fuzzymonkey

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: Problem with reading in files on mac.
« Reply #4 on: March 25, 2010, 06:13:30 pm »
How do I run it form a console?

Offline Smirftsch

  • Multiple posting newcomer
  • *
  • Posts: 22
Re: Problem with reading in files on mac.
« Reply #5 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