Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am unable to load binary files and create new binary with some part of the content from the read file. I used GFileoutputStream and g_file_load_contents but as soon as zero comes it takes it as terminating character which i don't want.I want to read even zeros and then extract data bit wise. Any help would be highly appreciated.

What I have tried:

C
void file_selected(GtkWidget *filechooserbutton, gpointer data,GtkTextBuffer *buffer)
{   
        filename=gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(filechooserbutton));
    g_file_get_contents(filename, &contents, NULL, NULL);

    text1 = g_substr(contents,0,value);     

    gint DataHeaderNum=96;
    gint value2=value+DataHeaderNum;
    text2 = g_substr(contents,value,value2);        

    gchar *filler;
    filler=g_strnfill (DataHeaderNum,'0');
    //g_print(filler);
    gint DataNum=2048-DataHeaderNum;
    gint value3=value2+DataNum;
    text3 = g_substr(contents,value,value3);
    g_strlcat(filler,text3,2048);
    text3=g_strdup(filler);
    //g_print(text3);


    gchar *full;
gchar *name="SpaceCraftHeader";
gchar *name2="DataHeader";
gchar *name3="Data";

full=g_strdup(filename);
g_strlcat(full,name,50);

GFile *file=g_file_new_for_path(full);
GFileOutputStream *output=g_file_replace(
            file,NULL,FALSE,
            G_FILE_CREATE_NONE,
            NULL,NULL);

g_output_stream_write(G_OUTPUT_STREAM(output),
            text1,strlen(text1),NULL,NULL);

g_output_stream_close(G_OUTPUT_STREAM(output),NULL,NULL);
}
Posted
Updated 15-Jun-16 0:22am
v2

1 solution

You are using string related functions in your code. These will of course stop at null bytes which are used to indicate the end of string.

If you want to process binary data you can use standard C file IO functions like the FILE based functions (fopen, fclose, fread, fwrite) or the file descriptor based functions (open, close, read, write).

If the files are not too big it is common to determine the file size, allocate a buffer with that size (usually of a byte type like unsigned char) and read the whole content into that buffer.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900