By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,044 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,044 IT Pros & Developers. It's quick & easy.

How to avoid NULL character when using fwrite

P: 93
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.
Sep 21 '10 #1
Share this Question
Share on Google+
5 Replies

Expert Mod 5K+
P: 8,916
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.
Sep 21 '10 #2

Expert 100+
P: 2,398
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.
Sep 21 '10 #3

Expert Mod 5K+
P: 8,916
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.
Sep 21 '10 #4

P: 93
fwrite(ptr2struc, sizeof(mystructype),1,OFILE);
fwrite("\n", 1,1,OFILE);
fwrite(ptr2struc, sizeof(mystructype),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
Sep 21 '10 #5

Expert Mod 5K+
P: 8,916
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.
Sep 22 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.