Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8682295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:44:09+00:00 2026-06-12T21:44:09+00:00

In C, when using the FILE struct from stdio.h , the current working directory

  • 0

In C, when using the FILE struct from stdio.h, the current working directory will be relative to the build directory if the programmer attempts to open a file.

Is there a setting of some sort (maybe a compiler flag) which I can use to allow the CWD to be set to the source directory at compile or run time? Preferably a method that is cross-platform from Windows to *nix, but if that’s not doable let’s just say *nix.

I’m working in Linux currently, with GCC/G++, using qmake without the Qt libraries.

Also, the main reason I’m using the C FILE IO method as opposed to C++’s std::ifstream is just personal preference, in case anyone asks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:44:10+00:00Added an answer on June 12, 2026 at 9:44 pm

    In your .pro file you can define the source directory as a macro, then chdir() to it in your source code.

    # myproject.pro
    DEFINES+=SRCDIR=\\\"$$PWD\\\"
    
    # myapp.cpp
    int main(int argc, char** argv) {
        if (chdir(SRCDIR)) {
            perror("chdir to " SRCDIR);
        }
        // ... rest of code
    }
    

    Getting it to work on Windows might involve fiddling with the number of backslashes in the .pro file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read from a file from using C. #include <stdio.h> struct record{
I'm Using FILE * F opened in _sfopen () I'm reading from the file
I've been using File.ReadAllText() to open a CSV file, but every time I forget
I need to play Music Library files using file URL, that I will set
I am trying to send audio file from one computer to other using socket
Here I implemented code for file download from server. its working fine. Now I
I have written a binary file using a struct as follows: struct block{ char
I'm working on porting my open source particle engine test from SDL to SDL
I am using file cache in symfony to store my data for some time
I am using File IO, to save a file in local system(i.e PC C:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.