20

I want the user to give me the full path where the file exists and not just the file name. How do I open the file this way?

Is it something like this:

ifstream file;
file.open("C:/Demo.txt", ios::in);

This doesn't seem to work.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188

5 Answers5

22

Normally one uses the backslash character as the path separator in Windows. So:

ifstream file;
file.open("C:\\Demo.txt", ios::in);

Keep in mind that when written in C++ source code, you must use the double backslash because the backslash character itself means something special inside double quoted strings. So the above refers to the file C:\Demo.txt.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • All windows compilers that I'm aware of support the use of the forward slash in file names, and it should be used in preference to the backslash for portability. –  May 09 '09 at 10:36
  • 11
    Win32 supports either "\" or "/" for paths, unless you are bypassing the path canonicalisation (paths starting with "\\?\"). See http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx – Richard May 09 '09 at 10:39
  • 2
    Neil, this has nothing to do with the compiler. The windows shell treats forward slashes the same way as back slashes. This has been the case since the early days of NT. – shoosh May 09 '09 at 10:39
  • The windows shell (if by that you mean cmd.exe) is in no way involved here. –  May 09 '09 at 10:44
  • 2
    It doesn't even have anything to do with the shell, it's up to the kernel (Richard has it right). I said "normally" because you can still run into problems when using forward slashes because some programs interpret arguments starting with a forward slash as command line options instead of file names. You are correct that for just opening files, either forward or back slash should be fine. Convention prefers backslash. – Greg Hewgill May 09 '09 at 10:46
14

You can use a full path with the fstream classes. The folowing code attempts to open the file demo.txt in the root of the C: drive. Note that as this is an input operation, the file must already exist.

#include <fstream>
#include <iostream>
using namespace std;

int main() {
   ifstream ifs( "c:/demo.txt" );       // note no mode needed
   if ( ! ifs.is_open() ) {                 
      cout <<" Failed to open" << endl;
   }
   else {
      cout <<"Opened OK" << endl;
   }
}

What does this code produce on your system?

2

The code seems working to me. I think the same with @Iothar.

Check to see if you include the required headers, to compile. If it is compiled, check to see if there is such a file, and everything, names etc, matches, and also check to see that you have a right to read the file.

To make a cross check, check if you can open it with fopen..

FILE *f = fopen("C:/Demo.txt", "r");
if (f)
  printf("fopen success\n");
phoad
  • 1,801
  • 2
  • 20
  • 31
1

For those who are getting the path dynamicly... e.g. drag&drop:

Some main constructions get drag&dropped file with double quotes like:

"C:\MyPath\MyFile.txt"

Quick and nice solution is to use this function to remove chars from string:

void removeCharsFromString( string &str, char* charsToRemove ) {
   for ( unsigned int i = 0; i < strlen(charsToRemove); ++i ) {
      str.erase( remove(str.begin(), str.end(), charsToRemove[i]), str.end() );
   }
} 

string myAbsolutepath; //fill with your absolute path
removeCharsFromString( myAbsolutepath, "\"" );

myAbsolutepath now contains just C:\MyPath\MyFile.txt

The function needs these libraries: <iostream> <algorithm> <cstring>.
The function was based on this answer.

Working Fiddle: http://ideone.com/XOROjq

Community
  • 1
  • 1
jave.web
  • 13,880
  • 12
  • 91
  • 125
1

A different take on this question, which might help someone:

I came here because I was debugging in Visual Studio on Windows, and I got confused about all this / vs \\ discussion (it really should not matter in most cases).

For me, the problem was: the "current directory" was not set to what I wanted in Visual Studio. It defaults to the directory of the executable (depending on how you set up your project).

Change it via: Right-click the solution -> Properties -> Working Directory

I only mention it because the question seems Windows-centric, which generally also means VisualStudio-centric, which tells me this hint might be relevant (:

jwd
  • 10,837
  • 3
  • 43
  • 67