Opening file in a sub-folder (fstream)

This is a discussion on Opening file in a sub-folder (fstream) within the C++ Programming forums, part of the General Programming Boards category; Im trying to open up a file to read in data. The code worked great when the file was in ...

  1. #1
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69

    Opening file in a sub-folder (fstream)

    Im trying to open up a file to read in data. The code worked great when the file was in the same folder as the program. The problem is that I want the files to be in a sub-folder called "Data". ive tried adding in the folder name but it just returns a failure to open file message

    Code:
    fstream load;
     
     load.open("\\Data\\pword.pwd", ios::in); //open file
     if(load.fail())
    	{
    	 cout<<endl<<"File Open Failed!";
    	}

    the bit in Bold is the code ive added to try and make it look in the sub folder but it isnt happy!


    Any ideas what ive done wrong?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    "\\Data\\pword.pwd"
    Is from the root of your current drive

    "Data\\pword.pwd"
    Is from your current working directory

  3. #3
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    Thanks Salem! It Works great.

  4. #4
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    You just need to put a dot before the path so the computer knows it is relative to your current dirctory make sure that Data directory has already been created as well.
    Code:
    load.open(".\\Data\\pword.pwd",ios::in);
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  5. #5
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    Quote Originally Posted by manofsteel972
    You just need to put a dot before the path so the computer knows it is relative to your current dirctory make sure that Data directory has already been created as well.
    Code:
    load.open(".\\Data\\pword.pwd",ios::in);
    Thanks! That method also worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  2. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM
  3. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21