Re: Writing a structure into a file



On Jun 24, 7:13 pm, "zaheer...@xxxxxxxxx" <zaheer...@xxxxxxxxx> wrote:

But the file.txt is containing junk values when i open it . Please let
me know what is wrong in my program.

with stdio.h included, I am getting the output you expected.
80000 40000 12345
80000 40000 12345
Your issue is already addressed. Your code appears to be broken in
more than one ways but the issue of seeing junk is that you are using
a text editor to analyze the file. Use a binary/hexadecimal editor and
you can check that you are getting the right values and some junk
because of your 3 in fwrite. (If you haven't used any binary/hex
editor, it will be all greek and latin with all the endian-ness and
stuff to worry about)

My compiler warns about your typedef:

bin.c:10: warning: useless storage class specifier in empty
declaration

Using a static code checker can let you know other issues which you
are ignoring ( not checking the return value for fopen, fread, fwrite
etc). In my case, lint has got this to say.

Splint 3.1.1 --- 19 Jul 2006

bin.c: (in function main)
bin.c:25:28: Possibly null storage fp passed as non-null param:
fwrite (..., fp)
A possibly null pointer is passed as a parameter corresponding to a
formal
parameter with no /*@null@*/ annotation. If NULL may be used for
this
parameter, add a /*@null@*/ annotation to the function parameter
declaration.
(Use -nullpass to inhibit warning)
bin.c:24:8: Storage fp may become null
bin.c:25:3: Return value (type size_t) ignored: fwrite(&A, sizeo...
Result returned by function call is not used. If this is intended,
can cast
result to (void) to eliminate message. (Use -retvalother to inhibit
warning)
bin.c:26:3: Return value (type int) ignored: fclose(fp)
Result returned by function call is not used. If this is intended,
can cast
result to (void) to eliminate message. (Use -retvalint to inhibit
warning)
bin.c:28:27: Possibly null storage fp passed as non-null param: fread
(..., fp)
bin.c:27:8: Storage fp may become null
bin.c:28:3: Return value (type size_t) ignored: fread(&B, sizeof...
bin.c:29:3: Return value (type int) ignored: fclose(fp)


lint is not related to the language C, but the output I have posted is
related to your C problem.

.



Relevant Pages

  • Re: splint: where is the memory leak?
    ... a warning from splint on this bit of code: ... The storage may be released or new aliases ... (Use -temptrans to inhibit warning) ...
    (comp.lang.c)
  • Why does the kernel build fail out of the box?
    ... kernel, even though I have been building UNIX kernels since 1979. ... :248: Warning: indirect lcall without `*' ... io_apic.c:169: storage size of `entry' isn't known ...
    (RedHat)
  • How to duplicate a build of the kernel in the RPM
    ... :248: Warning: indirect lcall without `*' ... io_apic.c:169: storage size of `entry' isn't known ... io_apic.c:874: warning: implicit declaration of function `apic_read' ...
    (RedHat)
  • How to duplicate a build of the kernel in the RPM
    ... I am trying to build the same kernel which came with the RPM ... :248: Warning: indirect lcall without `*' ... io_apic.c:169: storage size of `entry' isn't known ...
    (RedHat)
  • RE: Receiving notification on exchange storage limits
    ... Click to select one or all of the following check boxes under Storage ... Issue warning at: Select this check box to warn the user that their ... in the Warning message interval box or click Customize. ... Use System Policies to Configure Mailbox Storage Limits in Exchange ...
    (microsoft.public.windows.server.sbs)