passing a non string type to "fputs"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mahmoodn
    New Member
    • May 2009
    • 77

    passing a non string type to "fputs"

    Hi
    I have a struct
    Code:
    struct b_entry_t {
       uint32_t source;  
       uint32_t target;
       int counter;  
    };
    and I have created an instance of that
    Code:
    struct b_entry_t *entry;
    Somewhere in my program I want to write it to file with fputs. The problem is how can I do that?
    Code:
    fputs (entry, pFile);
    I get this warning
    Code:
    warning: passing argument 1 of ‘fputs’ from incompatible pointer type
    /usr/include/stdio.h:682: note: expected ‘const char * __restrict__’ but argument is of type ‘struct b_entry_t *’
    and after executing the program the content of the file doesn't contain uint32_t numbers. Seems that it is a binary file!!

    How can I fix that? thanks
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Your program data (in this case, a struct b_entry_t) can't be directly represented in a file. The structure has ... structure. There are a certain number of fields. Each field as a particular size and a particular type.

    You need to pick some encoding scheme for how your program data will be represented in the file. There are any number of possible encoding schemes, but they generally fall into two categories: a binary representation or a textual representation. Your choice of fputs suggests that you want a textual representation.

    Consider the following snippet:
    Code:
    struct b_entry_t { 
       uint32_t source;   
       uint32_t target; 
       int counter;   
    }; 
    struct b_entry_t *entry;
    ...
    entry->source = 1066;
    entry->target = 1492;
    entry->counter = 1941;
    put_b_entry_t(entry);
    What would you like the output to look like?

    Comment

    • mahmoodn
      New Member
      • May 2009
      • 77

      #3
      I want to write those fields in a plain text with fputs (is there another way? I don't know...)

      As in your example, the output.txt should looks like
      Code:
      1066     1492     1941

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        If you want to write binary data to a file as binary use fwrite. If you want to write binary data to a file as text use fprintf.

        Comment

        • mahmoodn
          New Member
          • May 2009
          • 77

          #5
          No as I said, the data are some uint_32t numbers and I want to write to a text file.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Read Banfa's message again. .

            Comment

            Working...