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

  • Home
  • SEARCH
  • 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 6110017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:28:07+00:00 2026-05-23T14:28:07+00:00

I need to load some text (shader code) from a file that is placed

  • 0

I need to load some text (shader code) from a file that is placed next to my library file. This way, I can #include TextureBloomer.h in several projects, and TextureBloomer will take care of loading its shader without needing to set paths through configuration or environment variables. The directory hierarchy looks like this:

libs/libName/src/TextureBloomer/TextureBloomer.{h,cpp}
libs/libName/src/TextureBloomer/shaders/shader_name.{frag,vert}
apps/mine/appName/src/[several .h files that #include "TextureBloomer/TextureBloomer.h"]

I’ve been looking around for ways to do this, but all I found was posts saying “you can’t”, or “why would you even want to do that?” I found a (hacky) solution that works for me, but I’ve seen other posts that say using __FILE__ is bad juju (with no justification). So, the question is a) what’s so bad about this, and b) is there a way to improve it, eg. so I don’t have to have “TextureBloomer.cpp” in the source file in case I change its name.

string filename ("shaders/shader_name.frag");
string currentFile (__FILE__);
currentFile.replace(currentFile.find("TextureBloomer.cpp"), 18, "");
file.open((currentFile + filename).c_str());

EDIT:

I’m on OSX, compiling with XCode. I only really need it to work there, but other *nixes wouldn’t hurt.

  • 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-05-23T14:28:08+00:00Added an answer on May 23, 2026 at 2:28 pm

    You are using that file as sort of a “resource” of your program. A simple option is in these cases to generate automatically a .cpp module that contains the source you need as string literals… i.e. if your source code is for example

    void foo(int x, int y)
    {
       return x + y;
    }
    

    you generate a .cpp that contains

    const char *shader_src[] = {
        "void foo(int x, int y)",
        "{",
        "    return x + y;",
        "}",
        NULL };
    

    This generation requires only a few lines of say Python code and can be probably added to your project makefile so that in case anything changes in the original source the file is regenerated automatically.

    Doing this way you will end up with an executable that will include this resource internally and there will be no need to distribute an external file.

    If on the other hand you need that file to be changed by the user without recompiling the program then an external file is the only option, but how to find a directory where it is located is highly system dependent. Just consider for example that on certain systems it’s perfectly possible to delete the file of a program that is currently in execution… in this case there is no directory on any disk where the program is currently residing.

    The reason that there is no way in C++ to portably access the source code directories at runtime is because the C++ language clearly separate the two phases of “compile time” and “run time” even at a philosophical level.

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

Sidebar

Related Questions

I need to be able to load the entire contents of a text file
I need to modify a (xml-)file from Apache Ant. loadfile task allows to load
I'm creating a custom ViewController. This VC needs to load some data that is
To complete some testing I need to load the 64 bit version of an
I have a two dimensional array that I need to load data into. I
The SQL-LDR documentation states that you need to do a convetional Path Load: When
I'm creating some text boxes on my form programmatically which I need to reference
I need to digitally sign some text in python using a private key stored
I need to load about 60 images, each on a different page, in to
I have a situation where I need to be able to load assemblies in

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.