0

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.

4
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by vmanes
0

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 :)

0

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


Any ideas, are welcome.

Thanks

0

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.

0

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"
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.