Hi, can someone please help clarify what would be the right way to embed a text file into the exe file? I have several text databases. I do not want them to remain as separate files, and would like them to be include into the exe. In Java, I can simply copy the text files into the project resource folders and then Java will compile them together with the executable. Is there a similarly simple and cross-platform way in C++? Thanks so much!
Have you tried compiling the text file in the resource folder for the .exe? If it doesnt work, you can simply change d file xtension for your text file of database, I use .db mostly. But i see no reason why the txt files shouldnt compile alongside d whole executable.
I may be doing something wrong but whenever I move the exe file from the project folder, it does not find the text file. So, the text file is obviously not embedded. Are there any settings in Code::Blocks I am missing?
And which compiler (not IDE) and platform(s) are you working with?
The usual Windows-specific way would be to bind the files into the resource segment. But it might be possible to use the objcopy approach used for Linux binaries (using the MinGW version of objcopy.)
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <windows.h>
#include <cstdio>
#include "resource.h"
usingnamespace std;
int main() {
string key = "300";
string result;
std::string line ;
HRSRC hRes = FindResource(0, MAKEINTRESOURCE(IDR_DATA1), MAKEINTRESOURCE(MYRESTYPE));
while( std::getline( hRes, line ) && line.find(key) != 0 );
if( line.find(search_string) != 0 )
{
std::cerr << "file was not found\n" ;
result = "N/A" ;
}
else{
result = line + '\n' ;
}
hRes.close();
cout << "The result is :" << result << endl;
system("PAUSE");
return 0;
}
Please note that the only experience I have is with Java, and I tried to replicate what I have found on Internet. Unfortunately, there is not a lot and the above is obviously wrong as it is not working.
Without the lines starting from "while..." it works perfectly well now, that is it prints the entire dataset. However, there is some problem in this line:
while( getline( res_data, line ) && line.find(keyIn) != 0 );
Error: no matching function for call.
I cannot call the getline function like this?
Oh, and one more silly question. So, if I have several more datasets plus a bunch of jpg files to incorporate into my application, can I have all of them in one resource file? Is it a good practice or should I have them in different resource files?
Thank you so much!
while( getline( res_data, line ) && line.find(keyIn) != 0 );
with
1 2 3
istringstream iss(res_data); // from <sstream>
while( getline( iss, line ) && line.find(keyIn) != 0 );
can I have all of them in one resource file?
Yes, that makes sense here; unless you have lots of resource (which is prob a bad sign) it's best to stick to a single .rc file (as it's easy to see what's going on.)