473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.
Sep 21 '10 #1
5 10400
Banfa
9,065 Expert Mod 8TB
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
donbock
2,426 Expert 2GB
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
Banfa
9,065 Expert Mod 8TB
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
FILE *OFILE = NULL;
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
Banfa
9,065 Expert Mod 8TB
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

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

Similar topics

4
by: Terry | last post by:
I'd like to build a table-less layout for a very simple page. The layout should look a bit like this (view with fixed font) +-------------+ ID: 2984884 | | Last Name: Doe | ...
2
by: Greg Buckley | last post by:
I have come across an interesting problem. Let me first state that I am not a fluent ASP programmer. I am a network engineering trying to port an existing app. The app in question is currently...
13
by: Seth Spearman | last post by:
Hey guys, I have the following code: '****************************************************** If Not Me.NewRecord Then Dim rs As DAO.Recordset Dim strBookmark As String Set rs =...
2
by: Miguel Dias Moura | last post by:
Hi, i have this line code to hide a table if a database field value is empty: <table runat="server" visible='<%# dataSetPerson.FieldValue("Book", Container) <> "" %>' ... What i want is to...
5
by: Joper | last post by:
Hi, I'm getting the ORA-00911:invalid character when trying to do a query to a Oracle database from a Excel VBA Macro. I'm using the following code extraction: ----BEGIN CODE----- Sub...
5
by: VancouverMike | last post by:
Hi there, I am writing a web app using asp.net 1.1 and C#. This web app is launched in another win32 application and this app needs to pass information to my web app. But this win32 application...
3
by: Vivienne | last post by:
I am using VS 2005. In a project when I was trying to write some unsigned char into a file, I used fwrite() and fputc(). But I found whenever I tried to write 0x0a, fwrite() function automaticly...
1
by: TP | last post by:
Hi everybody, All my problem is in the title. If I try: $ python -c 'print "foo",' It does not change anything, surely because the line return is added by "python -c".
4
by: Highlander2nd | last post by:
Hello there. I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models. When I store mesh data, I...
2
by: alexus | last post by:
fwrite(fopen('temp.xml','w+'), print_r(simplexml_load_string(curl_exec($test))),true); i for some reason get "1" inside of my temp.xml
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.