Thread: Reading a file from a subfolder

  1. #1
    Registered User
    Join Date
    Dec 2018

    Reading a file from a subfolder

    Hello, I'm trying to make a simple text based rpg game thingy and I don't want to clutter my working directory with dozens of text files to I neatly packed them into a sub folder in the which is located in the working directory. However I cannot figure out what am I doing wrong as it says that "files does not exist" and that is the same for all text files. Reading a file from a subfolder-untitled-png
    Last edited by Tesla_Boi; 12-23-2018 at 05:39 AM.

  2. #2
    Registered User
    Join Date
    Dec 2018
    Well apparently I just removed the "/Text Based RPG" part and added ".." before the "/Text Files" part. Can anyone explain why this worked and the other way did not?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well the "current working directory" can vary depending on how you launch the program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    /Path would be considered an absolute path.

    I assume you're working on Windows. In that case "/Text Based RPG/Text Files/" implies C:\Text Based RPG\Text Files (replace C:\ according to situation). To get a path relative to the current working directory (i.e. next to your executable), you simply start with the name.

    ├── build
    │   ├── install.bat      // path: "../install.bat"
    │   ├── app
    │   │   ├── program.exe  // ← your program & (usually) working directory
    │   │   ├── config.ini   // path: "config.ini" or "./config.ini"
    │   ├── data
    │   │   ├── blob.bin     // path: "../data/blob.bin"
    ├──            // path: "../../"

  5. #5
    Registered User
    Join Date
    Dec 2018
    Ohhhh, I see now. So what I was doing was telling the program that all the text files were in C:\Text Based RPG\Text Files, while my goal was to make it work wherever I move the whole Project Folder "Text Based RPG". But with the ".." before "/Text Files" and the removal of "/Text Based RPG" seemed to do the trick.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-27-2013, 12:24 PM
  2. Replies: 0
    Last Post: 03-21-2013, 03:31 PM
  3. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  4. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  5. Reading flat file and generating tagged file
    By AngKar in forum C# Programming
    Replies: 4
    Last Post: 03-24-2006, 08:29 AM

Tags for this Thread