How to avoid NULL character when using fwrite

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Girish Kanakagiri
    New Member
    • May 2007
    • 93

    How to avoid NULL character when using fwrite

    Through fwrite I am writting to a file & I have closed &
    re-opened a file in "a"(append) mode & using multiple
    fwrite function one after another on to a same file.

    It is working fine but evrey time after fwrite has written to the file at the end of the file it is inserting lot of NULL char(s) as well. I dont want these
    Null char. How can I get rid of this?

    Thanks in Advance.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    We will need to see the code you are using to write the data, but most likely you are writing more than you need to.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      By the way, there can be NULL pointers, but not NULL characters. This refers to the NULL macro defined by stdio.h (among others). That is, NULL is an actual identifier that can appear in your program.

      There can however be null characters. Here, "null" is a descriptive term (such as "pointer" or "function") that describes elements of your program without the term actually appearing in your program.

      I realize that explaining this pedantic distinction does nothing to solve your problem.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        In ASCII the name NUL specifically refers to a character with a value of 0. But not this is not a mis-spelling of NULL, as Don says NULL is a pointer term.

        All ASCII characters in the range 0 - 31 are "control" characters and have a name with a 2 or 3 letter abbreviation.

        Comment

        • Girish Kanakagiri
          New Member
          • May 2007
          • 93

          #5
          FILE *OFILE = NULL;
          fwrite(ptr2stru c, sizeof(mystruct ype),1,OFILE);
          fwrite("\n", 1,1,OFILE);
          fwrite(ptr2stru c, sizeof(mystruct ype),1,OFILE);
          o/p file->
          sj sljflsfjlfkl //o/p of 1st fwrite
          \0 \0 \0
          sfjslfjl //o/p of 3rd fwrite
          \0 \0
          I am getting null characters [\0] being written to the o/p file .this is not the expected char to be printed

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            I assume that you actually call fopen somewhere rather than use OFILE with a value of NULL in the fwrite calls?

            It would really help to know what the actual structure of mystructtype is to be able to accurately explain the output but I do not doubt the the output is because the code you have written instructs the computer to give that output.

            However what is clear is you are expecting printable (readable) ASCII characters to be written to the file but you are using a function (fwrite) that writes binary data.

            Not only that you are just dumping an entire structure and expecting the output to be printable ASCII characters but the only way for that to occur would be for the structure to only contain arrays of char and for every member of every array to have been assigned a valid ASCII printable character, i.e. no strings since they end in a \0.

            I suspect to get your file to print correctly you will need to do something a little more intelligent printing the individual members of the structure using the fprintf function.

            Comment

            Working...