0

I need to write unsigned long long integer values to a text file. When I used fprintf() it works perfectly but, takes time. I have profiled my application and fprintf() takes the half of all time. I have made a research and I saw that, fwrite() is more effective than fprintf(). But I couldn't managed how to use it?

My code with fprintf()

fprintf(out, "%llu %llu\n", min, minv);

My code with fwrite()

fwrite(&min,sizeof(unsigned long long int),1, out);
fputs(" ",out);
fwrite(&minv,sizeof(unsigned long long int),1, out);
fputs("\n", out);

fwrite() is faster the first one but when I opened the file, the data is like

¾²ö㄀㄀㄀ `àÔàUü
ؾ{4-㄀㄀㄀ "¤#sÏ$P
 oD/㄀㄀㄀ 
-5X®Z

How could I perform fwrite() properly in this situation?

  • 5
    `fwrite` was called "properly". You misunderstand that `fprintf` and `fwrite` do different things. `fprintf` prints a character representation, `fwrite` prints the raw data. Pick one. (And I don't think that you should be concerned about output performance here. A speed optimisation that breaks your code isn't worth anything.) – M Oehm Oct 25 '15 at 19:15
  • Try opening it with a hex editor. – Martin James Oct 25 '15 at 19:21
  • If you're going to insist on using `fwrite()`, you first have to convert the `unsigned long long` value to a string — perhaps using `snprintf()`? — and then write the appropriate length string to the file with `fwrite()`. Of course, it is far easier just to use `fprintf()` to do the formatting and writing, but if you want to beat it, that's what you'll need to do. If using `snprintf()` isn't OK (it shares a lot of code in common with `fprintf()`), then you'll have to write your own conversion function; there isn't a standard function to convert `unsigned long long` to a string. – Jonathan Leffler Oct 25 '15 at 19:23
  • Post how `out` was opened - critical in proper use of `fwrite()`. – chux - Reinstate Monica Oct 25 '15 at 19:27
  • @chux out = fopen("D:\\output/data.out","w"); opened like that –  Oct 25 '15 at 19:59

2 Answers2

2

... need to write unsigned long long integer values to a text file ...

The typical method to write an unsigned long long is the below. Note that the file is open in text mode.

out = fopen("data.out","w");
fprintf(out, " %llu", x);

To write faster, consider writing the text file file in binary mode. This will lose end-of-line translation, but may increase performance.

out = fopen("data.out","wb");
fprintf(out, " %llu", x);

If the compiler is weak, converting the unsigned long long to a string may further improve. A non-standard conversion function is needed. Some ideas here

out = fopen("data.out","wb");
char buf[50];
buf[0] = ' ';

size_t size = my_ulltoa(&buf[1], x);  // returns size of the array used.
fwrite(buf, 1, 1 + size, 1, out);

OP's use of the below failed as it wrote a binary (e.g. 4 or 8 byte) representation to the text file, which is not readable as characters.

out = fopen("data.out","w");
// fails
fwrite(&min,sizeof(unsigned long long int),1, out);

Far faster is to write the binary representation to a binary file - but that is not readable as text.

// Works for non-text files
out = fopen("data.out","wb");
fwrite(&min, sizeof min, 1, out);
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

fwrite() is faster because it isn't actually converting the value to a character representation that you could view with a text editor. It's just writing the raw byte data of the integer to the file.

If you want to use fwrite(), you have to convert the integer value to a string beforehand, although I think you'll be hard pressed to find a performance gain over simply using fprintf().

PC Luddite
  • 5,883
  • 6
  • 23
  • 39
  • Well, I'm using fprintf() for writing a line and I have millions of lines. If I found something that can improve the performance, It would be awesome. What do you suggest me in this situation? What could be the more acceptable theory for it ? –  Oct 25 '15 at 19:41
  • 1
    @AlwaysNewbie If you want to beat `fprintf()`'s performance, you'll have to write your own function for converting an `unsigned long long int` to a string so it can be written to a file with `fwrite()`. – PC Luddite Oct 25 '15 at 21:34