I have to read a file (for a string) and the file is available only at the compilation time, not in the run time.

Is there any possibility to read the file during compilation and initialize a variable with the value read from the file.

I have tried different methods, but doesn't work for me. Can anybody please tell me how can i do this?


Thanks in advance.

The file is available at compilation time.. but not run time. As far as I can think, the only way this is possible is to use the #include directive, assuming the file contains C/C++ code. Other than that, I have no idea :)

the file is a pure text file, consists of just a single line.


Any ideas, are welcome.

Thanks

Hmm, write a little program that will read the file and store it, properly formatted as a declaration of a string with that content as the initialization, saving as a .h file to be included in your program?

Run that prior to compiling the primary application.

sounds good.

Thanks for the idea.

You're welcome. Let us know how it works out.

Just copy and paste into a string array.

There is a very hacky, nested way of doing it:

#include <stdio.h>
int main()
{
    char text[] = {
                #include "text.txt" 
                };
    
    printf("%s\n", text);
    getchar();
    return(0);
}
text.txt:
"text and stuff"

That works, assuming the text in the file is enclosed in doublequotes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.