简体   繁体   English

C ++如何将字符串块写入文件?

[英]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. 我有两个非常大的数字(每个最多23位数),我想尽可能快地将它们打印到文件中。 The lines in the file should look like: 文件中的行应如下所示:

number1[space]number2[\\n] eg. number1 [space] number2 [\\ n]例如。 123456789999999 99999965454644 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(). 我的方法是将行收集到一个大缓冲区中,当缓冲区已满时,使用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. 最大的问题是,我不知道如何从这两个数字创建一个字符串并将它们保存到一个字符串/ char数组中。 As I mentioned the length of the string is variable, it can be only 3 characters or even 50. 正如我所提到的,字符串的长度是可变的,它只能是3个字符甚至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:我尝试了两种附加方法:

1) write numbers immediately into the file using fprintf(f, "%lld %lld\\n", num1, num2); 1)使用fprintf(f, "%lld %lld\\n", num1, num2);立即将数字写入文件fprintf(f, "%lld %lld\\n", num1, num2);

2) using ofstream exactly the same way as @somnium mentioned. 2)使用ofstream与@somnium提到的完全相同。

I did measure both of them, and surprisingly, the 1) approach is 2x faster than the second. 我确实测量了它们,令人惊讶的是,1)方法比第二种方法快2倍。

If you are using C++ and not plain C you can use : 如果您使用的是C ++而不是普通的C,则可以使用:

#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(f,”%lld%lld \\ n“,num1,num2)有什么问题;”? fprintf uses buffered IO, so there is no point in adding another layer of buffering. fprintf使用缓冲IO,因此添加另一层缓冲是没有意义的。 If you want to increase the buffer size, you can use setvbuf() for this. 如果要增加缓冲区大小,可以使用setvbuf()。

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. 但是,最快的方法可能是避免任何缓冲并将所有数据传递给writev(),这需要用一个系统调用写入一组缓冲区。 However, dealing with partial writes (which writev is allowed to do) is not trivial. 但是,处理部分写入(允许writev执行)并非易事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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