store binary data in program...

This is a discussion on store binary data in program... within the C Programming forums, part of the General Programming Boards category; Hi, I was wondering if there was a way to store some data in the executable. This is a large ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    230

    store binary data in program...

    Hi,
    I was wondering if there was a way to store some data in the executable. This is a large amount of data so I can't just define an array and set some values like this:
    Code:
    short data[X] = {fa, 36, 3c, ...};
    I also tried appending the data using a text editor (even though I'm not sure how I'll find it...) but the program stopped working...
    is there any way of doing this?
    I might not be a pro, but I'm usually right

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    On windows, you can use resources. Really the best idea is to just put it in a separate file though.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    I might just resort to keeping the data in another file, but I don't mind trying resources. Any links I can check out (don't worry I'll google it too, I don't need to be spoon fed).
    I might not be a pro, but I'm usually right

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    uh >_> can't find it right now
    Last edited by Doodle77; 03-22-2008 at 08:06 AM.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    http://msdn2.microsoft.com/en-us/lib...42(VS.85).aspx
    http://msdn2.microsoft.com/en-us/lib...46(VS.85).aspx
    the resource type you want is RT_RCDATA (application-defined resource).
    For getting resources into your application, use RC (if you use msvc++, if you use dev-cpp/mingw use windres, which uses the same types of files).
    Last edited by Doodle77; 03-22-2008 at 08:00 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It really depends on what the source of the data is. There's absolutely no problem in initializing fairly large arrays. Your example is broken in that it uses hex notation without the prefix of 0x, but other than that, as long as the compiler is happy with the size of the array in general [e.g a 16-bit compiler may limit a single array to 64KB, a 32-bit compiler is definitely limited to 4GB, but other limits may apply, either compiler or system dependant limits [a windows system won't allow more than 2GB under normal circumstances, and that includes your code as well as data]].

    Also bear in mind that stack space is always more limited than global data space.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Sorry about the hex mistake, i wrote the example really fast and didn't catch it.

    About the source of the data, I will be copying the data from a file. That's why (I think) I can't just define an array...
    I might be wrong though... would it work if i copied the binary data using a text editor into a block of memory, whether by using an array or something like malloc()?
    e.g.
    Code:
    char *bin_data = "data here...";
    I have a feeling the problem would be with the text editor, but how else can i copy the data? hex wouldn't work because i would need to change each character to include the escape sequence. So what else can I do if none of my solutions would work?
    I might not be a pro, but I'm usually right

  8. #8
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Write a program to create your array. I prefer Perl for such jobs.
    The basic idea is to write a file that contains:
    Code:
    int my_array[] = {
    ...
    };
    And between the brackets just spit out comma seperated 0xNN whatever values.
    [Edit]
    You can use a raw copy like the one you display if the data is textual.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Well, the problem is, it's not textual, but it's too long to write in hex.
    I tried opening the file in notepad (it definately has non-printable characters by the way), copying the contents, creating a new file and dumping them into it, but that didn't work, so I guess copying them to the array won't work either...

    And I'm sorry but what do you mean by "Write a program to create your array"? do you mean another program? other than the one I'm writing now?
    Am I right in thinking that I should write a program that would write the array -source- with all the data in it (I actually never thought of that... I think it's a good idea )
    I might not be a pro, but I'm usually right

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    OK. I started tried writing some code and this is what ended with:
    If you don't want to to see all the code (it's a bit lengthy) you can just look at the main() function, that's where I think the problem is...
    Code:
    // Basically a program that reads a binary file (you can just drag the file onto the program) and
    // outputs its contents to a file (named by the user) in HEX in an array definition
    // e.g.
    // short file[filesize] = {0x21,0xb3, 0x5f, ...};
    
    #include <stdio.h>
    
    #define VALUES_PER_LINE 20
    
    void clrInBuff(void);
    void rmExt(char fileName[]);
    int rmNwLn(char string[]);
    int fndExtLcn(const char fileName[]);
    long int sizeofFile(FILE *file);
    
    
    int main(int argc, char *argv[])
    {
       FILE *inFile;  // binary file to be read from
       FILE *outFile; // output file with array definition
       
       char outFilename[FILENAME_MAX+1];
       char inFilename[FILENAME_MAX+1];
       long int inFileSize;
       
       int valuesWritten;   // variable to count number of values written
       long int i;
       long int freadResult;
       
       if (argc != 2)
       {
          printf("Enter input filename: ");
          fgets(inFilename, FILENAME_MAX, stdin);
          rmNwLn(inFilename);
       }
       else  strcpy(inFilename, argv[1]);
       
       printf("Enter output filename: ");
       fgets(outFilename, FILENAME_MAX, stdin);
       rmNwLn(outFilename);
       
       printf("opening files\n");
       if ((inFile = fopen(inFilename, "rb")) == NULL)
       {
          printf("Error opening \"&#37;s\"\n", inFilename);
          clrInBuff();
          return 1;
       }
       else printf("\"%s\" opened successfully\n", inFilename);
       
       if ((outFile = fopen(outFilename, "wt")) == NULL)
       {
          printf("Error creating \"%s\"\n", outFilename);
          clrInBuff();
          return 1;
       }
       else printf("\"%s\" created successfully\n", outFilename);
       
       inFileSize = sizeofFile(inFile) / sizeof(char);
       
       rmExt(outFilename);
       printf("Started outputting to files\n");
       fprintf(outFile, "char %s[%ld] = \n{", outFilename, inFileSize);
       
       valuesWritten=0;
       //do
       {
          char *buf;
          
          buf = (char *) malloc( sizeof(char) * inFileSize);
          freadResult = fread(buf, sizeof(char), inFileSize, inFile); 
          
          for (i=0; i<freadResult; i++)
          {
             if (valuesWritten == VALUES_PER_LINE) fputc('\n', outFile);
             if (fprintf(outFile, "%#x, ", buf[i]) < 0)
                printf("Error in fprintf\n");
             //printf("%#hx, ", buf[i]);
             ++valuesWritten;
          }
          free(buf);
       }// while (freadResult == inFileSize);
       fputs("\b\b}", outFile);
       
       printf("Closing files\n");
       fclose(inFile);
       fclose(outFile);
       clrInBuff();
       
       return 0;
    }
    
      
       
    
    
    /* Clears input buffer */
    void clrInBuff(void)
    {
    	char c;
    
    	/* keep reading characters until '\n' is found (nothing after that) or EOF is reached */
    	while ( ((c=getchar()) != '\n') && (c != EOF) ) ;
    }
    
    
    /* Searches for and removes '\n' from a string */
    int rmNwLn(char string[])
    {
    	int i;
    
    	for (i=0; string[i] != '\n' && string[i] != '\0'; i++)
    		;
    
    	if (string[i] == '\n')
    	{
    		string[i] = '\0';
    		return i;
    	}
    
    	else
    		return EOF;
    }
    
    
    /* returns size of file */
    long int sizeofFile(FILE *file)
    {
    	long int i;
    	fseek(file, 0, SEEK_END);
    	i = ftell(file);
    	fseek(file, 0, SEEK_SET);
    
    	return i;
    }
    
    
    /* remove extension of file */
    void rmExt(char fileName[])
    {
    	int i;
    
    	i = fndExtLcn(fileName);
    
    	fileName[i] = '\0';
    }
    
    
    /*	return location of extension in file name (after '.').
    	returns '\0' if no extension.
    */
    int fndExtLcn(const char fileName[])
    {
    	int i;
    
    	/* Keep searching backwards until '.' is found (extension started) */
    	for (i = strlen(fileName); fileName[i] != '.' ; i--)
    		if (!i)
    		{
    			return '\0';
    		}
    	i++;
    	return i;
    }
    For some reason, the output file is not even created... I can't really think of anything to fix this :'(

    EDIT: modified the code to allow manual filename input. When I use arguments (by dragging the file onto the executable) fopen succeeds but the file is not created. However when I input the file name manually, it works fine...
    I really don't understand what's going on :S
    Last edited by Abda92; 03-23-2008 at 12:51 PM.
    I might not be a pro, but I'm usually right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  2. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21