简体   繁体   中英

C++ How to write block of strings to file?

I have two really big numbers (max 23 digits/each) and I'd like to print them into a file as fast as it can be done. The lines in the file should look like:

number1[space]number2[\\n] eg. 123456789999999 99999965454644

My approach is collecting the lines into one big buffer, and when the buffer is full, write it into the file, using fwrite().

The biggest problem is, that I don't know how to create one string from those 2 numbers and save them into one string/char array. As I mentioned the length of the string is variable, it can be only 3 characters or even 50.

const int OUT_BUFFER_SIZE = 65536;
string out_buffer[OUT_BUFFER_SIZE];
int out_size = 0;
FILE* output_file = fopen("result.out", "w");

void print_numbers(FILE* f, long long num1, long long num2)
{
  const int ELEMENT_SIZE = 50;

  char line[ELEMENT_SIZE];
  sprintf(line, "%lld %lld\n", num1, num2);
  out_buffer[out_size] = line;
  out_size++;

  if (out_size == OUT_BUFFER_SIZE)
  {
     fwrite(out_buffer, ELEMENT_SIZE, OUT_BUFFER_SIZE, f);
  }
}

fclose(output_file);

Is there any other way how to solve this problem? Thanks.

EDIT1: I've tried two addtional approaches:

1) write numbers immediately into the file using fprintf(f, "%lld %lld\\n", num1, num2);

2) using ofstream exactly the same way as @somnium mentioned.

I did measure both of them, and surprisingly, the 1) approach is 2x faster than the second.

If you are using C++ and not plain C you can use :

#include <iostream>
#include <fstream>
using namespace std;

void print_numbers(const std::string fname, long long num1, long long num2)
  ofstream myfile;
  myfile.open (fname);
  myfile << num1 << " " << num2 << '\n';
  myfile.close();
  return 0;
}

That does the necessary buffering underlying already.

What's wrong with using "fprintf(f, "%lld %lld\\n", num1, num2);"? fprintf uses buffered IO, so there is no point in adding another layer of buffering. If you want to increase the buffer size, you can use setvbuf() for this.

However, the fastest way would probably be to avoid any buffering and pass all data to be written to writev(), which takes a set of buffers to be written with one system call. However, dealing with partial writes (which writev is allowed to do) is not trivial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM