views:

67

answers:

3

considering visual C++ compiler, Lets say I've got a file with whatever extension and it contains 100 bytes of data which are exactly the data that I want to initialize an array of char data type with a length of 100 characters with, Now apparently one way is to read those data out of file by using I/O file classes or APIs at run-time but what I want to know is that, is there any way using directives or something to tell the compiler I want to put that data in a right place in my application image files at compile time and compiler should go read those data out of that file?

+4  A: 
  • Write a program that reads the 100 byte data file and generates as output, a file, with c++ code/syntax for declaring an array with the 100 bytes in the file.
  • Include this new generated file(inline) in your main c++ file.
  • Call the c++ compiler on the main c++ file.
letronje
A: 
#include "filename"

or am I missing something obvious?

Dipstick
This won't work--he says the file is raw data (not code)
egrunin
That is because the question has been edited since I answered.
Dipstick
@Dipstick_No, Edition didn't have anything to do with changing the core issue.
Pooria
You didn't originally state that the data was in binary format. If it is then just convert it to comma separated hex ascii before hand then include that.
Dipstick
@Dipstick_Actually I did and that's why others proposed solutions regarding the matter.
Pooria
+2  A: 

You do this with a resource in a Windows program. Right-click the project, Add, Resource, Import. Give the custom resource type a name. Edit the resource ID, if necessary. Get a pointer to the resource data at runtime with FindResource and LoadResource.

Hans Passant
Does the compiler examine the resource at compile time ?
letronje
The resource compiler, yes. Not much examining going on, it just embeds the bytes.
Hans Passant
You tell that still using a function like FindResource or LoadResource is required and that would be done at run-time, I meant a solution at compile-time.
Pooria