From: "David" Newsgroups: comp.os.msdos.djgpp References: <39A9AA50 DOT FA3AAE59 AT home DOT com> Subject: Re: __DATA__ in c? Lines: 146 X-Newsreader: Microsoft Outlook Express 4.72.3110.5 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 Message-ID: Date: Mon, 28 Aug 2000 00:39:02 -0400 NNTP-Posting-Host: 209.214.202.83 X-Trace: news3.atl 967437593 209.214.202.83 (Mon, 28 Aug 2000 00:39:53 EDT) NNTP-Posting-Date: Mon, 28 Aug 2000 00:39:53 EDT To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com yhvhboy1 wrote in message <39A9AA50 DOT FA3AAE59 AT home DOT com>... >now, what i am trying to do is save contents of a binary file >in c, using the djgpp gcc compiler and compile the file >right in with my c program so i can read from it >and use its contents... >kind of like zip2exe, except with the ability >to write the file where i want and do other things >besides just unzip a zipfile-- >in other words write more code around it- >not just do one thing (save a file). >and i'd like to do it without having to >store it into an array (would be a problem with say, a .iso >cd image - or other large files, etc.) You have to store it to an array. >and i don't have a clue how to do it. >comp.lang.c told me to come here and ask >since it's beyond the core c language. Here's some sample code that will read in a file, and create a C-code file declaring a constant array: ======== Cut-here ======== #include #include /***************************************************************** * * openFile * * Convenience wrapper for fopen ****************************************************************** */ FILE *openFile(const char *fileName, const char *mode) { FILE *f; f = fopen(fileName, mode); if (f == NULL) { printf("Could not open file %s\n", fileName); exit(1); } return(f); } /***************************************************************** * * main ****************************************************************** */ int main(void) { FILE *dataFile, *codeFile; unsigned char c; size_t n; int count; dataFile = openFile("data.dat", "rb"); // Open for read, in binary mode codeFile = openFile("code.c", "w"); // Open for write, in text mode // Print variable declaration to file fprintf(codeFile, "const char data[] = {\n"); while (!feof(dataFile)) { // Print 3 leading spaces for readability if (count == 0) { printf(" "); } // Read data from the file 1 character at a time fread((void *) &c, sizeof(c), 1, dataFile); // Print character to file in hexadecimal fprintf(codeFile, "%02X, ", (unsigned int) c); // Start new row every eight values count = (count + 1) % 8; if (count == 0) { fprintf(codeFile, "\n"); } } // End variable declaration fprintf(codeFile, "\n};\n"); fclose(dataFile); fclose(codeFile); return(0); } ======== Cut-here ======== If you save this code as "codefile.c", you can compile it with this command: gcc codefile.c -o codefile.exe For a "data.dat" file with "Hello, world!" in it, I got this output in "code.c": ========== CODE.C ========== const char data[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x2C, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21, 0x0D, 0x0A, 0x0D, 0x0A, 0x0A, }; const long dataSize = 18; ========== CODE.C ========== Here's some sample code that can access the data: ========== TEST.C ========== #include extern char data[]; extern long dataSize; int main(void) { long indx; for (indx = 0; indx < dataSize; indx++) { printf("%c", data[indx]); } } ========== TEST.C ========== To make the test code, type these commands gcc -c -Ic:\djgpp\include test.c -o test.o gcc -c -Ic:\djgpp\include code.c -o code.o gcc -o test.exe test.o code.o When you run test.exe, you will get this: Hello, world! May God bless you, David