Including files into executable

A

Ad

Hello,

I've written a simple program that sends a postscript snippet file to a
printer via LPT1. I want to include the file within the executable however.
How would I go about this?

Adrian
 
M

Mike Wahler

Ad said:
Hello,

I've written a simple program that sends a postscript snippet file to a
printer via LPT1. I want to include the file within the executable
however. How would I go about this?

Write a simple 'code generator', i.e.
write a short program that reads this file and writes
a source code file which defines an array or container
containing the text of the postscript file.
Use an #include directive at an appropriate place
in your orignal program to insert the generated
source code.

Roughly:

ifstream in("whatever.ps");
ofstream out("ps.cpp");

out << "const char *ps[] = {\n"

while(getline(in, line))
{
out << '"' << line << '"' << ',';
}

out << "\n};\n";


// yoursimpleprogram.cpp
#include "ps.txt";
/* etc */
int main()
{
for(size_t i = 0 i < sizeof ps / sizeof *ps; ++i)
print_routine(ps);

/* etc */
}

-Mike
 
R

rf

Ad said:
Hello,

I've written a simple program that sends a postscript snippet file to a
printer via LPT1. I want to include the file within the executable however.
How would I go about this?

Include it as a resource.
 
J

Jonathan Mcdougall

Mike said:
Ad said:
Hello,

I've written a simple program that sends a postscript snippet file to a
printer via LPT1. I want to include the file within the executable
however. How would I go about this?

Write a simple 'code generator', i.e.
write a short program that reads this file and writes
a source code file which defines an array or container
containing the text of the postscript file.
Use an #include directive at an appropriate place
in your orignal program to insert the generated
source code.

Roughly:

ifstream in("whatever.ps");
ofstream out("ps.cpp");

out << "const char *ps[] = {\n"
;

while(getline(in, line))
{
out << '"' << line << '"' << ',';

Aggregate initialization cannot end with a comma.
}

out << "\n};\n";


// yoursimpleprogram.cpp
#include "ps.txt";

That should be "ps.cpp".
/* etc */
int main()
{
for(size_t i = 0 i < sizeof ps / sizeof *ps; ++i)
print_routine(ps);

/* etc */
}


Well, it's *kinda* roughly :)


Jonathan
 
M

Mike Wahler

Jonathan Mcdougall said:
Mike said:
Ad said:
Hello,

I've written a simple program that sends a postscript snippet file to a
printer via LPT1. I want to include the file within the executable
however. How would I go about this?

Write a simple 'code generator', i.e.
write a short program that reads this file and writes
a source code file which defines an array or container
containing the text of the postscript file.
Use an #include directive at an appropriate place
in your orignal program to insert the generated
source code.

Roughly:

ifstream in("whatever.ps");
ofstream out("ps.cpp");

out << "const char *ps[] = {\n"
;

while(getline(in, line))
{
out << '"' << line << '"' << ',';

Aggregate initialization cannot end with a comma.

Actually, it can.
That should be "ps.cpp".

It should be whatever OP wants. ".cpp" or
any other 'file name extension', or lack of
one, has no significance to the language.
/* etc */
int main()
{
for(size_t i = 0 i < sizeof ps / sizeof *ps; ++i)
print_routine(ps);

/* etc */
}


Well, it's *kinda* roughly :)


Yup. :)

-Mike
 
M

Marcus Kwok

Mike Wahler said:
Roughly:

ifstream in("whatever.ps");
ofstream out("ps.cpp");

out << "const char *ps[] = {\n"

while(getline(in, line))
{
out << '"' << line << '"' << ',';
}

out << "\n};\n";

I'm not familiar with the PS format so the point may be moot, but won't
this mess up if there is an embedded double-quote in the line?

Of course, you did say this was "rough" :)
 
M

Mike Wahler

Marcus Kwok said:
Mike Wahler said:
Roughly:

ifstream in("whatever.ps");
ofstream out("ps.cpp");

out << "const char *ps[] = {\n"

while(getline(in, line))
{
out << '"' << line << '"' << ',';
}

out << "\n};\n";

I'm not familiar with the PS format so the point may be moot, but won't
this mess up if there is an embedded double-quote in the line?

Yes it would. But I'd rather leave dealing with such
details to the OP.
Of course, you did say this was "rough" :)

Very. :)

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top