0

I am attempting to use a variable filename, to load a file. All of these files are stored in the same subdir to my program. I'm doing this like so:

int fileNum = whatever;
string fName;
stringstream ss;
ss << "Files\\Foo" << fileNum << ".csv";
fName = ss.str();

fstream foo (fName.c_str());

The problem I'm having is that the compiler refuses to replace "\\" with '\'. Using '\' produces a compiler error, of course, but when I debug, watches shows that fName gets a value of "Files\\Foo1.csv", which of course won't open anything.

Joe
  • 398
  • 4
  • 15
  • 3
    You can use forward slash `/` instead of `\\` - it would work as well and does not require escaping – mvp May 11 '13 at 19:38
  • did you try giving full path? – smttsp May 11 '13 at 19:38
  • Make sure the file is reachable and you have the appropriate privilege to open it. – stardust May 11 '13 at 19:39
  • 9
    Your `Files\\Foo1.csv` is just as it should be. The debugger shows the slash escaped, but there will be one in the actual string. My bet is that you aren't in the directory you think you are. – Mats Petersson May 11 '13 at 19:40
  • 1
    For example, running out of the IDE can put you in a different directory than the executable. – chris May 11 '13 at 19:43
  • @MatsPetersson is probably correct do: `GetCurrentDir(size, path); std::cerr << pwd() << path << "\n";` Then you can at least see where the application thinks it is. http://stackoverflow.com/q/143174/14065 – Martin York May 11 '13 at 19:48

1 Answers1

0

The problem is not related to slashes. Debugger escapes slashes in watches, so you should see \\ there, there is nothing wrong here. You can cout the file name to see if it's correct.

Most probably you have no the specified file in your working directory. Check your IDE settings: what is the directory for executable (usually it's called Output path) and what is working directory during the debug.

Also try running the program without IDE.

nullptr
  • 11,008
  • 1
  • 23
  • 18
  • Yeah, it works ouside of the IDE, but when I need to debug I also need access to those files - the path I'm supposing (project/bin/debug/) is the only path specified as output by the IDE - that's where the files are. Any idea on how to find out the working directory when running under the debugger? Edit: Didn't see comment above, will try soon. – Joe May 11 '13 at 19:54
  • I used a solution from here (http://stackoverflow.com/questions/875249/how-to-get-current-directory) - this showed that my .exe was in fact in the file I thought it was... – Joe May 11 '13 at 21:49
  • The link suggests using `GetModuleFilePath`, this would show you the path to the .exe file, but not its working directory. Did you try `GetCurrentDirectory()` ? Does it show the correct path? – nullptr May 11 '13 at 22:01
  • Maybe later... I thought an executables path was where it was executed from? – Joe May 11 '13 at 23:18
  • Each process has its current directory. When you start the process from command line or by double-clicking its executable file, its current directory is usually set to its location. But an IDE can assign another working (current) directory. In any case it would be more reliable to specify full paths to the files (the path can be calculated from the .exe path). – nullptr May 12 '13 at 07:43