Re: Faster way to write in a file



LilacSkin wrote:
Hi,

Currently, I write an signed long long in a file with the fprintf
function:

No. You don't. You write the textual representation of the signed long
long value to the file. I will return to this point shortly.

signed long long * pData = NULL;
unsigned long long k = 0;
unsigned long DataAvail = 0 ;

pDataRx = (signed long long *) malloc ( sizeof(signed long long ) *
(65536*16*8) / 8);

for ( k = 0; k < DataAvail/8; k ++ ) {
fprintf ( pFilec, "%lli\t", pData[k]);
}

This snipped code is clearly incomplete, so tells us virtually nothing.

pDataRx is undefined. DataAvail is 0, so the loop will not be entered...

The problem is that function is very very slow !

Compared to what? How do you know?

In several forums, I saw that fwrite function is better,

Than what? In what way? Do you know (by measuring) that fprintf is the
cause of your problem?

As I pointed out above, fprintf is converting your signed long long
values to their textual representation. fwrite can't do that - it would
write the internal binary representation of the data.

I suppose you could write your own single-purpose function to convert
signed long long values into text and write that text with fwrite, and
that may perform better than fprintf...

but I don't know how to use it.

It (fwrite) is a simple enough function which is documented in the
standard and any good text.

Can you help me, please ?

You'd need to give us more information about the problem before we can
help you with the solution.
.



Relevant Pages

  • Re: confused abt file operations
    ... "Reply" at the bottom of the article headers. ... Either fprintf() or fwrite() can be used with either binary or text ... binary files. ...
    (comp.lang.c)
  • Re: Historical question, why fwrite and not binary specifier for fprintf?
    ... write, but no fwrite. ... There was putc(), which is portable. ... were added to the C standard to allow portable binary IO to files. ... standard C function fprintf to allow binary operations? ...
    (comp.lang.c)
  • Re: read and write binary file
    ... There can be other differences as well, and this is why C makes a clear distinction. ... C allows you to use fprintf on a binary stream and fwrite on a text stream. ...
    (comp.lang.c)
  • Re: convert decimal to hexadecimal number
    ... an integer valueand then after converting it into ... print it..so using fprintf along with %lx would not help me.for eg..if ... file, in binary mode, as a series of bytes, use fwrite. ... the underlying I/O package may care. ...
    (comp.lang.c)
  • Re: to tidy my file
    ... have it in the path setting as well, ... You can´t use fwrite to write this, use fprintf. ... Why is top-posting such a bad thing? ...
    (comp.soft-sys.matlab)