How do I open a C file with a relative path?

I am trying to parse a txt file and I want to reference it relative to my current directory location. If I put the file path in completely it will work but it I try to use ..\src\test.txt it wont find it. Is this not the proper way to reference a file relative up one directory?

Any help would be appreciated


If you put "..\src\test.txt" in a string constant, you need to double all the backslashes so that "\t" and "\s" are not interpreted as escape sequences. (Or you can use forward slashes instead, which is more portable.)


It will depend on what the current working directory is set to. By default it is the directory the executable resides in if you double-click the app from Explorer, or the current path the shell is in if started from a command prompt.

If test.txt is in c:\code\app\src and your application is in c:\code\app, the relative path "..\src\test.txt" is going to end up c:\code\src\test.txt (if launched from explorer).

Try printing the output of _getcwd before you try opening the file to verify what directory is the current working directory.


Assuming that your directory structure looks like:

 |       test.txt
 |       proj.c
         a.out  <- Working directory  

your relative path is correct; your working directory is actually on the same level as the text file.

If you really mean that the file is up one directory as you stated, like this: (Note: This is an awkward project structure)

    |   test.txt
    |   proj.c

or like this: (makes more sense)

|   test.txt
|       proj.c

Then the path you need is "../test.txt" or, equivalently, "../../project/test.txt"

A better location would be in a data directory, so your path would be "../data/test.txt"


I guess this is a windows VS vbuild (given the back slashed path)

VS will have put the binary in something like project\bin\debug. And when you run it in VS it will set the current WD to the location of the binary.


a) copy the file to the right place

b) change the project properties debug setup to say set the current path to the place where you expect the file to be (relatively)


  • Emacs shell: save commit message
  • Hamburgler.js button not working on mobile devices
  • How to break a string over multiple lines and preserve spaces in YAML?
  • Jackson Parser can't read backslash quotation marks in String
  • SQL substr function
  • How to rename file with a sequence that restarts if certain matches exist
  • Change font size to fit text inside button entirely?
  • c# database connection string
  • Rectangular Nesting - Convergence to optimal solution using Simulated Annealing
  • How to dispatch an KeyboardEvent with specific KeyCode
  • use of phpmailer class
  • Creating foreign key in MySQL
  • In Python, how should one extract the second-last directory name in a path?
  • How can I specify an auto_prepend_file in my .htaccess file without hardcoding the absolute path?
  • In C,why is definition of a global variable in a separate statement raising warning,but is OK for a
  • Custom URI routing by query string with CodeIgniter?
  • staticfiles and STATIC_URL with virrtualenv - django
  • Convert date string value from U.S. to native Date with current locale U.S.?
  • How to monitor MQ within ITRS Geneos
  • Why are `colMeans()` and `rowMeans()` functions faster than using the mean function with `lapply()`?
  • SQL Server 2008 R2 - Islands and Gaps [closed]
  • Pass nested C++ vector as built-in style multi-dimensional array
  • How to upload files in php using html
  • 'include' of functions in groovy scripts
  • Query pkg-config variable through autotools
  • Can XOR be expressed using SKI combinators?
  • Better Indy for Dephi 2007
  • Use of this Javascript
  • Linq Objects Group By & Sum
  • Sony Xperia Z Tablet not found by adb
  • How to format a variable of double type
  • Matrix multiplication with MKL
  • Buffer size for converting unsigned long to string
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • Change div Background jquery
  • File not found error Google Drive API
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory
  • Converting MP3 duration time