Strings2h

Strings2h

Post by Michael Ghe » Fri, 27 Feb 1998 04:00:00



I have had one before, but I am looking for a program that will
take a text file and turn it to a c string.

Michael

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Michael Ghens                       Small Planet Connections

                                    http://www.spconnect.com/~michael    

 
 
 

Strings2h

Post by Neil Moor » Sun, 01 Mar 1998 04:00:00



> I have had one before, but I am looking for a program that will
> take a text file and turn it to a c string.

> Michael

A.  This is the wrong newsgroup for the question.

B.
Assuming you have an ANSI C compiler that supports strings of
arbitrary length,

#! /usr/bin/perl -w
print "const char *string =\n";

while (<>) {
        chomp;
        s/"/\\"/g;
        s/(.*)/"$1\\n"/;
        print "    $_\n";

Quote:}

print ";\n";
__END__

If you want an array of strings (probably a better choice),

#! /usr/bin/perl -w
print "#include <stdlib.h>\n";
print "const char *stringv[] = {\n";

while (<>) {
        chomp;
        s/"/\\"/g;
        s/(.*)/"$1\\n",/;
        print "    $_\n";

Quote:}

print "    NULL };\n";
__END__

--