views:

44

answers:

1

This is really strange. Absolute path doesn't work for both ifstream and ostream. It works when I use a relative path like so:

ofstream out;
out.open("file2.txt");
string river = "i love cheese";

if(!out){
    cout << "error"; // have breakpoint set here
} else {
    out << river; // have breakpoint set here (stops here when debugging)
}

out.close();

But when I use an absolute path, it doesn't. I am well aware of needing to use "\" for the slash and I've tried using "/" instead and it still doesn't work.

ofstream out;
out.open("C:\\file2.txt"); // also tried "C:/file2.txt"
string river = "i love cheese";


if(!out){
    cout << "error"; // have breakpoint set here (stops here when debugging)
} else {
    out << river; // have breakpoint set here
}

out.close();

I really need it to work with an absolute path since that is what is provided to the function and the input and output files won't always be in the same folder as the binary.

+1  A: 

What's you're operation system? Windows 7 does not allow to create files on C:\. You could create new folder on C:\, for example C:\temp\ and try this code:

std::ofstream out;  
out.open("C:\\temp\\asd.txt" );
if( ! out )
{
    std::cout << "1";
}
if ( !out.is_open() )
{
    std::cout << "2";
}
out.close();

This works fine. But when you try to create fiile on C:\ , it will print "12".

Kiril Kirov
I'm using Vista, but I was just using "C:\" as a simplified example. The document I'm looking for input/output is deep in "My Documents" (which will have a lengthy path surrounded with double quotes).
alex
I guess Vista is the same as windows 7, as we're talking about such privileges. Try other directory, it should work. Try in "My Documents", as you want.
Kiril Kirov
This is the folder where the executable is and it still won't write:\"C:\\Users\\Alex\\Documents\\Visual Studio 2008\\Projects\\outputProject\\Debug\\file2.txt\"
alex
Okaay, I don't need to know you're directory. Did you try to create file there? It works for me (in my directory). Also, as there's the exe, you don't really have to use the complete path. Anyway, just try it in you code (: It should work. And why you're escaping the double quotes ? You do **not** have to, just use **out.open( "C:\\Users\\Alex\\Documents\\Visual Studio 2008\\Projects\\outputProject\\Debug\\file2.txt");**
Kiril Kirov
I'm escaping the double quotes because of the spaces in "Visual Studio 2008". The way this program works is it gets an absolute path from some other function so the files it needs aren't always going to be in the same folder as the executable. I did create a file there using ostream and fopen, but it fails when evaluating !out.
alex
Sorry, I think I didn't understand you at all.. What's the problem with the spaces in "Visual Studio 2008"? If you have the whole string with the complete path, you don't need to enclose the path with double quotes, even if it contains white spaces.
Kiril Kirov
I thought it needed it because using the Windows command prompt you need to wrap the path in quotes if there are spaces. I removed the quotes and it worked! Thank you for suggesting that.
alex
I'm glad that I helped (:
Kiril Kirov
Could you, please, mark this question as answered ("accept" the answer), not to be shown in the "Unanswered" section of stackoverflow.com? Thanks (:
Kiril Kirov