views:

812

answers:

9

So in Code::Blocks in Ubuntu (latest).

I have a project in which I load a file and read a number from it.

#include <fstream>
using namespace std;
int main(){
    ifstream in("data/file.t");
    int n;in>>n;
}

now with a cout<<n it shows -1203926 (and other random numbers) though the number in the file is 0.

data is where the binary is(I mean data and binary are in the same folder(Program)) and I was expecting the path to be relative like in Windows... but only if I put the full path /home/csiz/Desktop/C++/ep0/Program/data/file.t it will get me a 0.

Can you tell me how to make it a relative path? I would prefer something so that in Windows the code can compile without any changes.

+1  A: 

If the binary is in the data directory use file.t instead of data/file.t.

xsl
Not to mention that the path is relative to where you *run* the binary from, not where it's located...
workmad3
data and binary are in the same folder, codeblocks runs the exe like its being ran from that folder (I also tried running from the terminal...)
csiz
A: 

I think Boost Filesystem library would help, altough I have got no experience with it (just with other boost libraries - those worked great)

bernhardrusch
A: 

To get portable paths for windows and linux you will need to either write your own methods to adjust the file paths or preferably use library like boost::filesystem.

I've used boost::filesystem in my projects and I recommend it. It's easy to create path, check if file exists, create directories and so on. Maybe steep learning curve for beginner c++ programmer but the basic stuff like creating file path should be easy.

stefanB
so another downvoter walked past downvoting for fun (?) ... since there's no comment about what issue he found in my suggestion of cross platform library to access the path without code change on ubuntu and windows ... as far as I know boost is good for that kind of think ...
stefanB
Same here - but I stopped thinking about those kind of things..
bernhardrusch
A: 

The only reason this relative path works in "Windows" is that your IDE executes the binary in .. instead of in data/. If you run MSVC you can be sure of it.

So, change code to

#include 
using namespace std;
int main() {
  ifstream in("file.t");
  int n;
  in>>n;
}

then change IDE current directory to data/ -> port complete! :)

Jonas Byström
A: 

This means that your current directory when you run the program is not set to where it should be (it's probably at your home directory). Try running the program like this:

cd whatever_dir_is_above_data
./my_prog

EDIT: Oops, you probably also need ./ before the program, since . is probably not in your path (I forgot it because I always set it in my path so I don't have to type it).

Zifre
I don't get this:csiz@csiz-desktop:~/Desktop/C++/ep0/Program$ ep0bash: ep0: command not foundep0 is in Program directory
csiz
+2  A: 

The path is relative to the current working directory, not to the directory your application is under.

A simple solution would be to have a SH script that changes the working directory to your application's directory, and then executes your app, like so:

$!/bin/sh

cd `dirname $0` # changes the working dir to the script's dir

./application-name # executes your application

# no need changing back to your previous working directory
# the chdir persists only until the end of the script

It's not uncommon for applications to have initialization scripts.

You could also do this inside your main C/C++ application. Since the path of the executable is passed in the main method's argv[0], you could do the same.

But I would advise against it, because when you're redistributing your application on Linux, data files are usually placed in a different directory (usually /var/lib) than your executables (usually /usr/bin).

So it's either an initialization script, or passing the path of your data directory in an environment variable, executing it like so ...

MY_APP_DATA_PATH=/var/lib/my-app /path/to/executable
Alexandru Nedelcu
I think you mean `dirname` rather than `basename`
KayEss
Yeah, sorry. Fixed it.
Alexandru Nedelcu
A: 

Copy the /data directory to the bin directory of the project or where the binary is compiled.

relative paths are the same for linux and windows(yes exactly same usage, i use them everyday in my cross-platform application coding).

just check for CASE, i.e all linux paths are CASE-SENSITIVE. its something we miss when we move from Windows to Linux

nicko
A: 

After using the absolute path I found the mistake.

In codeblocks you can enter the working directory (that in wich it will launch the program) and I accidentally put a . in there.

csiz
A: 

Hi

I've been writing an application in MONO C# i don't know if this is the correct palce to ask but I need help, my problem is my aplication has to load some photos from my server or central machine which is running windows XP and i need to know how i have to write the photo's path. i mean for windows is somthing like this.

"\\MyServer\Photo'sFolder\picture.jpg"

under mono I've tried

"smb://MyServer/Photo'sFolder/picture.jpg"

but it didn't work and I have spent a lot of time looking for the answer without luck.

I really thak you in advance

diego
You should not post this as an answer to this old question, but ask it as a question on its own. The "Ask Question" button is in the top right. Also better add how exactly you tried to load the photos (which classes/method? Maybe add a code sample...) and what went wrong (error messages, exceptions?).
sth
Thank you actually I did it.
diego
Great, and it seems like your problem was solved there! So you could delete this post, since it's kind of in the wrong place and also obsolete by now...
sth