Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

writing a string to a file

Options
  • 23-03-2009 5:36pm
    #1
    Registered Users Posts: 26,579 ✭✭✭✭


    i'm trying to write a string to a file.

    here's the code
    void write_to_file(Widgets *w)
    {
        FILE *file = NULL;
        char* filename = "setup";
        gchar* fs = gtk_combo_box_get_active_text(GTK_COMBO_BOX(w->fs_combo));
        
        g_print("fs = %s\n", fs);
            
            
        /*open the file*/
        file = fopen(filename, "w");
        
        /*show error if cannot open file*/
        if(file == NULL)
        {
            show_error(w->window, "cannot open or create file", "please try again!");
        }
        else
        {
            fwrite(fs, 1, sizeof(fs), file);
            fclose(file);
        }
    
    }
    
    here's what's in the combo box

    "Ext 2 (Extended File System 2)"
    "Ext 3 (Extended File System 3)"
    "FAT 16 (File Allocation Table 16)"
    "FAT 32 (File Allocation Table 32)"

    using g_print("fs = %s\n", fs); will print out the full text of the string selected in the combo box into the console window. but when i go to write it to the file i just get the amount of text up to the first white space character.

    eg. i pick Ext 2, in the file i get:
    Ext
    
    there's a space in the above after Ext.

    the code compiles perfectly with no errors and no warning but i can't seem to figure out why it's bailing out, would it be because of the () contained within the string although i took them out and it still fails at that point?


Comments

  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    [PHP]fwrite(fs, 1, sizeof(fs), file);[/PHP]

    looks wrong to me.
    prototype

    [PHP]size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );[/PHP]

    also, when you say sizeof(fs), thats only size of char * which would be..4 bytes?

    should work like:

    [PHP]fwrite(fs,strlen(fs),1,file);[/PHP]


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    d'oh, of course.

    used sizeof(fs) instead of strlen(fs)


Advertisement