fstream

If I were to give a project to someone with commands from the fstream library, how would the file open if it's destination is from my computer?

i.e.
ifstream wordBank("C://Users//My name//"foo.txt");
Wouldn't I need to to give the receiver foo.txt? Thus, changing it's path?

I really don't even know if this makes sense, but any help would be greatly appreciated.
Last edited on
If your program runs on a different computer, it will look for "C://Users//My name//foo.txt" on their computer, failing to open the file if it doesn't exist. (So unless they have the same name as you, the above path would likely fail)

This is partially why full hardcoded pathnames are avoided. Instead you can use a relative path (just "foo.txt") and that will look in the current directory (typically the same directory as the exe, but when running from an IDE it might be the project directory instead).

Or you can prompt the user to supply the path/filename and leave it up to them to get it right.
Last edited on
Instead you can use a relative path (just "foo.txt") and that will look in the current directory (typically the same directory as the exe, but when running from an IDE it might be the project directory instead).


This helped me. Thank you.
Topic archived. No new replies allowed.