Code::Blocks Forums
User forums => Help => Topic started 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();
-
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.
-
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.
-
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.
-
How do I run it form a console?
-
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