Embed data in a C++ program - c++

I've got a C++ program that uses SQLite. I want to store the SQL queries in a separate file -- a plain-text file, not a source code file -- but embed that file in the executable file like a resource.
(This has to run on Linux, so I can't store it as an actual resource as far as I know, though that would be perfect if it were for Windows.)
Is there any simple way to do it, or will it effectively require me to write my own resource system for Linux? (Easily possible, but it would take a lot longer.)

You can use objcopy to bind the contents of the file to a symbol your program can use. See, for instance, here for more information.

Use macros. Technically that file would be source code file but it wouldn't look like this.
Example:
//queries.incl - SQL queries
Q(SELECT * FROM Users)
Q(INSERT [a] INTO Accounts)
//source.cpp
#define Q(query) #query,
char * queries[] = {
#include "queries.incl"
};
#undef Q
Later on you could do all sorts of other processing on that file by the same file, say you'd want to have array and a hash map of them, you could redefine Q to do another job and be done with it.

You can always write a small program or script to convert your text file into a header file and run it as part of your build process.

Here's a sample that we used for cross-platform embeddeding of files.
It's pretty simplistic, but will probably work for you.
You may also need to change how it's handling linefeeds in the escapeLine function.
#include <string>
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;
std::string escapeLine( std::string orig )
{
string retme;
for (unsigned int i=0; i<orig.size(); i++)
{
switch (orig[i])
{
case '\\':
retme += "\\\\";
break;
case '"':
retme += "\\\"";
break;
case '\n': // Strip out the final linefeed.
break;
default:
retme += orig[i];
}
}
retme += "\\n"; // Add an escaped linefeed to the escaped string.
return retme;
}
int main( int argc, char ** argv )
{
string filenamein, filenameout;
if ( argc > 1 )
filenamein = argv[ 1 ];
else
{
// Not enough arguments
fprintf( stderr, "Usage: %s <file to convert.mel> [ <output file name.mel> ]\n", argv[0] );
exit( -1 );
}
if ( argc > 2 )
filenameout = argv[ 2 ];
else
{
string new_ending = "_mel.h";
filenameout = filenamein;
std::string::size_type pos;
pos = filenameout.find( ".mel" );
if (pos == std::string::npos)
filenameout += new_ending;
else
filenameout.replace( pos, new_ending.size(), new_ending );
}
printf( "Converting \"%s\" to \"%s\"\n", filenamein.c_str(), filenameout.c_str() );
ifstream filein( filenamein.c_str(), ios::in );
ofstream fileout( filenameout.c_str(), ios::out );
if (!filein.good())
{
fprintf( stderr, "Unable to open input file %s\n", filenamein.c_str() );
exit( -2 );
}
if (!fileout.good())
{
fprintf( stderr, "Unable to open output file %s\n", filenameout.c_str() );
exit( -3 );
}
// Write the file.
fileout << "tempstr = ";
while( filein.good() )
{
string buff;
if ( getline( filein, buff ) )
{
fileout << "\"" << escapeLine( buff ) << "\"" << endl;
}
}
fileout << ";" << endl;
filein.close();
fileout.close();
return 0;
}

It's slightly ugly, but you can always use something like:
const char *query_foo =
#include "query_foo.txt"
const char *query_bar =
#include "query_bar.txt"
Where query_foo.txt would contain the quoted query text.

I have seen this to be done by converting the resource file to a C source file with only one char array defined containing the content of resource file in a hexadecimal format (to avoid problems with malicious characters). This automatically generated source file is then simply compiled and linked to the project.
It should be pretty easy to implement the convertor to dump C file for each resource file also as to write some facade functions for accessing the resources.

Related

Issue reading file with ifstream using absolute path

Hello stack overflow community. I came here as a last resort because i probably made a stupid mistake i cannot see myself.
The question im asking is for some reason when i try to read a file with an absolute path(or relative, you can see i tried that in my code) it cannot read the file for some unknown reason(atleast to me). This is a small thing for a big project im working on. Thank you guys!
main.cpp
#include <iostream>
#include <fstream>
#include <filesystem>
#include <unistd.h>
#include <string>
std::string openf() {
FILE* pipe = popen("zenity --file-selection", "r"); // open a pipe with zenity
if (!pipe) return "ERROR"; // if failed then return "ERROR"
char buffer[912]; // buffer to hold data
std::string result = ""; // result that you add too
while(!feof(pipe)) { // while not EOF read
if(fgets(buffer, 912, pipe) != NULL) // get path and store it into buffer
result += buffer; // add buffer to result
}
//I thought i needed to convert the absolute path to relative but i did not after all
// char cwd[10000];
// getcwd(cwd, 10000); // get cwd(current working directory)
// result = std::filesystem::relative(result, cwd); // convert the absolute path to relative with cwd
pclose(pipe); // cleanup
return result;
}
std::string readf(std::string filename){
std::string res;
std::ifstream file;
file.open(filename.c_str());
if(file.is_open()) {
while(file){
res += file.get();
}
}else {
std::cout << "failed to open file " + filename;
}
return res;
}
int main( void ){
std::string file = openf();
std::cout << file << std::endl;
std::string str = readf(file);
std::cout << str << std::endl;
return 0;
}
output
/home/meepmorp/Code/Odin/test/test.odin
failed to open file /home/meepmorp/Code/Odin/test/test.odin
It seems zenity, which you use as file chooser, outputs an extra newline after the file name, which you include in the name. In Linux, files can actually contain embedded newline characters in their name, and you actually try to open "test.odin\n" instead of "test.odin".

How do I read the output of a batch file into a string in C++

I'm trying to make a small program that will create a Batch file, do something in it and then return a string from it, and after it delete the Batch.
I would like to store the output of the batch file in the variable line.
I tried using getline() but I think it work with .txt files only. I can be wrong.
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
ofstream batch;
string line;
batch.open("temp.bat", ios::out);
batch <<"#echo OFF\nwmic os get caption /value\nwmic path win32_videocontroller get description /value\npause\nexit";
batch.close();
system("temp.bat");
remove("temp.bat");
};
In my code I simply using system with my Batch file. I'd like to use cout<<line.
I expect string called line would be equal to the output of the Batch file.
You need to redirect output when using system():
#include <cstdio> // std::remove(const char*)
#include <cstdlib> // std::system(const char*)
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
int main()
{
std::string foo_bat = "foo.bat";
std::string foo_out = "foo.out";
// Write the batch file
{
std::ofstream f( foo_bat );
f << R"z(
#echo off
wmic os get caption /value
wmic path win32_videocontroller get description /value
)z";
}
// Execute the batch file, redirecting output using the current (narrow) code page
if (!!std::system( (foo_bat + " | find /v \"\" > " + foo_out + " 2> NUL").c_str() ))
{
// (Clean up and complain)
std::remove( foo_bat.c_str() );
std::remove( foo_out.c_str() );
std::cout << "fooey!\n";
return 1;
}
// Read the redirected output file
std::unordered_map <std::string, std::string> env;
{
std::ifstream f( foo_out );
std::string s;
while (getline( f >> std::ws, s ))
{
auto n = s.find( '=' );
if (n != s.npos)
env[ s.substr( 0, n ) ] = s.substr( n+1 );
}
}
// Clean up
std::remove( foo_bat.c_str() );
std::remove( foo_out.c_str() );
// Show the user what we got
for (auto p : env)
std::cout << p.first << " : " << p.second << "\n";
}
WMIC is a problematic program when it comes to controlling the output code page, hence the weird pipe trick we use with system().
But, after all that, you should use the WMI API directly to get this kind of information.
A possible, though admittedly not ideal solution would be to have the batch file write it's output to a .txt file and then read in that file into your program. Look at this SO thread to see how to do this.

is it possible to grab data from an .exe file in c++?

I am new at C/C++,
So basically I want to call an .exe file that displays 2 numbers and be able to grab those two numbers to use them in my code.
To call the .exe file I've used the system command, but I am still not able to grab those two numbers that are displayed by the .exe file
char *files = "MyPath\file.exe";
system (files);
I think this is better aproach:
Here you just create new process, and you read data that process gives you. I tested this on OS X 10.11 with .sh file and works like a charm. I think that this would probably work on Windows also.
FILE *fp = popen("path to exe","r");
if (fp == NULL)
{
std::cout << "Popen is null" << std::endl;
}else
{
char buff[100];
while ( fgets( buff, sizeof(buff), fp ) != NULL )
{
std::cout << buff;
}
}
You need to escapr back slashes in C++ string literals so:
// note the double "\\"
char* files = "MyPath\\file.exe";
Or just use forward slashes:
char* files = "MyPath/file.exe";
Its not very efficient but one thing you can to with std::system is redirect the output to a file and then read the file:
#include <cstdlib>
#include <fstream>
#include <iostream>
int main()
{
// redirect > the output to a file called output.txt
if(std::system("MyPath\\file.exe > output.txt") != 0)
{
std::cerr << "ERROR: calling system\n";
return 1; // error code
}
// open a file to the output data
std::ifstream ifs("output.txt");
if(!ifs.is_open())
{
std::cerr << "ERROR: opening output file\n";
return 1; // error code
}
int num1, num2;
if(!(ifs >> num1 >> num2))
{
std::cerr << "ERROR: reading numbers\n";
return 1; // error code
}
// do something with the numbers here
std::cout << "num1: " << num1 << '\n';
std::cout << "num2: " << num2 << '\n';
}
NOTE: (thnx #VermillionAzure)
Note that system doesn't always work everywhere because unicorn
environments. Also, shells can differ from each other, like cmd.exe
and bash. – VermillionAzure
When using std::system the results are platform dependant and not all shells will have redirection or use the same syntax or even exist!

C++ fstream multiple input files

I am writing a simple program to take in two files. The terminal command line looks like this.
./fileIO foo.code foo.encode
When it runs, the second file is not read in. When I enter
./fileIO foo.code foo.code
it works. I can't seem to figure out why the second one is not opening. Any ideas? Thanks!
#include <fstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <map>
#include <string>
#include <cassert>
using namespace std;
int main( int argc, char *argv[] )
{
// convert the C-style command line parameter to a C++-style string,
// so that we can do concatenation on it
assert( argc == 3 );
const string code = argv[1];
const string encode = argv[2];
string firstTextFile = code;
string secondTextFile = encode;
//manipulate the first infile
ifstream firstFile( firstTextFile.c_str(), ios::in );
if( !firstFile )
{
cerr << "Cannot open text file for input" << endl;
return 1;
}
string lineIn;
string codeSubstring;
string hexSubstring;
while( getline( firstFile, lineIn ) )
{
hexSubstring = lineIn.substr(0, 2);
codeSubstring = lineIn.substr(4, lineIn.length() );
cout << hexSubstring << ", " << codeSubstring << endl;
}
//manipulate the second infile
ifstream secondFile( secondTextFile.c_str(), ios::in );
if( !secondFile )
{
cerr << "Cannot open text file for input" << endl;
return 1;
}
char characterIn;
while( secondFile.get( characterIn ) )
{
cout << characterIn << endl;
}
return 0;
}
One thing you might want to try is adding the close() call as is standard procedure after you're done using files. Sometimes issues arise with re-opening files if they were not closed properly in a previous run.
firstFile.close();
secondFile.close();
Also, you may try restarting the computer if there is some lingering file handle that hasn't been released.

Applying a regex search on each file in a folder

I have to write a program that will go through a given folder and use regex_search to find every instance of a certain string. I've got the regex_search working on it's own now, and I'm just trying to figure out how to go through each file. I want to attempt it using directory but am unsure where I would put it. Would I have to put the search through the file into my main method or would I have to create a seperate function outside of the main method for going through each file and call on it within the main method?
This is what I have now. Any tips you guys can give on how to approach this would be greatly appreciated!
Right now the function of it is to read an input text file and output a txt file that shows all the instances and the line number of each apperance. I am not required to see which lines they are on, use a particular file, or make an output file for this program, what I find will simply be printed to the console. I've left what I have now because I'm not sure if I'll checking each indivdual file in a similar fashion just with a different cariable name.
#include <iostream>
#include <regex>
#include <string>
#include <fstream>
#include <vector>
#include <regex>
#include <iomanip>
using namespace std;
int main (int argc, char* argv[]){
// validate the command line info
if( argc < 2 ) {
cout << "Error: Incorrect number of command line arguments\n"
"Usage: grep\n";
return EXIT_FAILURE;
}
//Declare the arguments of the array
string resultSwitch = argv[1];
string stringToGrep = argv[2];
string folderName = argv [3];
regex reg(stringToGrep);
// Validate that the file is there and open it
ifstream infile( inputFileName );
if( !infile ) {
cout << "Error: failed to open <" << inputFileName << ">\n"
"Check filename, path, or it doesn't exist.\n";
return EXIT_FAILURE;
}
while(getline(infile,currentLine))
{
lines.push_back( currentLine );
currentLineNum++;
if( regex_search( currentLine, reg ) )
outFile << "Line " << currentLineNum << ": " << currentLine << endl;
}
infile.close();
}
Reading a directory/folder is operating system dependent. In a UNIX/Linux/MacOS world, you use opendir(), and readdir():
#include <sys/types.h>
#include <dirent.h>
...
DIR *directory = opendir( directoryName );
if( directory == NULL )
{
perror( directoryName );
exit( -2 );
}
// Read the directory, and pull in every file that doesn't start with '.'
struct dirent *entry;
while( NULL != ( entry = readdir(directory) ) )
{
// by convention, UNIX files beginning with '.' are invisible.
// and . and .. are special anyway.
if( entry->d_name[0] != '.' )
{
// you now have a filename in entry->d_name;
// do something with it.
}
}