0
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream infile("text.txt", ios_base::in); // this works
    ofstream outfile("C:/output.txt", ios_base::out); // doesn't
    outfile << "hi";

    return 0;
}

No file is created. What is the problem?

Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
  • `/` is not a path separator on Windows. Have you tried `"C:\\output.txt"`? Does your user have privileges to create files in the volume root? – cdhowie Oct 10 '14 at 15:25
  • @cdhowie Yes, and yes. – Aviv Cohn Oct 10 '14 at 15:26
  • 1
    @cdhowie: Windows works fine with either forward or backward slash as a path separator when passed to functions. It's pretty much only the shell that demands a backslash. – Jerry Coffin Oct 10 '14 at 15:27
  • Try `ofstream outfile; outfile.exceptions(ofstream::failbit | ofstream::badbit); outfile.open("C:/output.txt", ios_base::out);` and see what the exception message is. – cdhowie Oct 10 '14 at 15:28
  • @cdhowie `iostream`s are not very exception-friendly: you won't likely get meaningful messages. I'd query `errno` instead. – edmz Oct 10 '14 at 15:30
  • 1
    Despite the assurance of the right privileges, I'm still betting that's the problem. Even running as admin with full rights to nearly everything, it doesn't work for me either (but works fine when I specify a different path instead of the root of the primary drive). – Jerry Coffin Oct 10 '14 at 15:32
  • cerr << strerror (errno) < endl ; – user3344003 Oct 10 '14 at 15:51
  • @user3344003 / black: [None of the fstream classes are required to set errno when something fails.](http://stackoverflow.com/q/839644/501250) This is not a portable way to check for errors. – cdhowie Oct 10 '14 at 15:57
  • We're talking about debugging here. Many do. – user3344003 Oct 10 '14 at 16:00
  • Mind changing the output file path to be relative (e.g. "output.txt")? If a relative path works, that would build a case for this being a permissions issue. – Cryo Oct 10 '14 at 17:39

0 Answers0