Re: not writing to file



John Smith <jsmith@xxxxxxxxxxx> wrote:
I try to write some data to a file. After some data had supposedly
been written to the file, I opened the file before the program
closed it and found it empty. What's wrong with my code? Thanks.

FILE *lptr
char *name;
int nNo;

.............
.............

lptr=fopen("myfile","w")

..............
..............

/* deep in a function */

sprintf(buffer, "%s\r\n",name);
printf("No is %d. Name is %s\n",nNo,buffer);

Why first copy everything to 'buffer' when printf() can print out
what 'name' is pointing directly?

/* they printed OK on screen, my way of checking things */

fwrite (name , 1 , sizeof(name) , lptr);

If 'name' is the variable you defined above then this call is probably
not what you wanted to do - it will write out as many chars as a char
pointer is long on your system, not the string. Replace 'sizeof(name)'
by 'strlen(name)' to write out the string (or use fprintf()).

fwrite (&nNo , 1 , sizeof(nNo) , lptr);

/* checked file without waiting for it to close and
/* found nothing in the file. It's empty. Why? */

As Adrian pointed out most standard C output functions store output
in their internal buffers before writing them to disk (at least as
long as there's enough place left in the buffers) in order to speed
up execution. To make sure things have got written to the disk either
call fflush() on the FILE pointer or 'unbuffer' it using setbuf() or
setvbuf() (or close the file).
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@xxxxxxxxxxx
\__________________________ http://toerring.de
.



Relevant Pages

  • Re: How git affects kernel.org performance
    ... throughput on a reasonably fast modern disk. ... Without hashing, the directory will be much smaller too, so readdir() ... previous one (4kB buffers, so if the previous getdents() happened to just ... static int create_file(const char *name) ...
    (Linux-Kernel)
  • Doesnt all the buffer pages have non-null page->private value??
    ... I modified my kernel source to trace all the buffers used for my disk. ... private flag set and private field pointing to circular buffer-heads. ... has proper page->private pointer and PagePrivatewrong? ...
    (comp.os.linux.development.system)
  • Re: Problem with va_ macros and arrays of arrays
    ... > the arrays passed to a ... > specific char, somewhat similar to what the standard function ... that with an array of struct, or possibly a pointer to a dynamic array ... > As I'm still a beginner in C without a copy of the standard I ...
    (comp.lang.c)
  • Re: Insufficient guarantees for null pointers?
    ... will the compiler know what the bounds are after converting that char * ... to an int *, if it could point to either of two arrays which happen to ... compares equal to the original pointer. ...
    (comp.std.c)
  • Re: when is typecasting (unsigned char*) to (char*) dangerous?
    ... > When are they not consistent? ... patterns that are valid as `unsigned char' might be invalid ... treated as "trap representations" and could cause your program ... Given a pointer to any data object, ...
    (comp.lang.c)