将文件放入C 中的数组中

put the file into an array in c++

本文关键字:数组 文件      更新时间:2023-10-16

我正在尝试读取txt文件,然后将其放入char数组中。但是,我可以读取包含不同字符的不同文件并将其放入数组中的不同文件。我可以创建一个动态数组以包含未知的字符长度。

您可以将一个未知大小的文件读取为:

的动态数据结构

std::vector更多信息在这里

另外,您可以使用new分配动态内存。但是,矢量至少对我来说更方便:(。

#include <vector>
#include <iostream>
#include <fstream>
int main(int argc, char **argv)
{
  std::vector<std::string> content;
  if (argc != 2)
  {
    std::cout << "bad argument" << std::endl;
    return 0;
  }

  std::string file_name (argv[1]);
  std::ifstream file(file_name);
  if (!file)
  {
    std::cout << "can't open file" << std::endl;
    return 0;
  }
  std::string line = "";
  while (std::getline(file, line))
  {
    content.push_back(line);
    line = "";
  }
  for (std::vector<std::string>::iterator it = content.begin(); it != content.end(); ++it)
      std::cout << *it << std::endl;
}

这是使用std :: vectors and std :: string

的解决方案

程序将文件名称为第一个参数,打开它,按行读取IT

每行都写在矢量

然后,您可以像我在功能末尾一样显示向量

编辑:由于C 11是新的Standars,因此程序使用C 11,因此您必须使用C 11(如果使用G (进行编译(g++ -std=c++11(

我刚刚测试了它的工作原理

可能有可用的库例程可为您提供文件的大小,而无需阅读文件的内容。在这种情况下,您可以获取大小并分配全尺寸的缓冲区,然后一次吸入整个文件(如果您的缓冲区是一个简单的char数组,请不要忘记添加一个并放入尾随的Nullchary]。/p>

最好的方法是使用malloc()realloc()free(),就像它是一个旧的C程序一样。如果您尝试使用std::vector,您将ch缩在最大rAM上,因为realloc()可以生长并缩小到位(生长在堆上取决于堆,而std::vector不能这样做。

特别是:

#include <iostream>
#include <tuple>
// TODO perhaps you want an auto-management class for this tuple.
// I'm not about to type up the whole auto_ptr, shared_ptr, etc.
// Mostly you don't do this enough to have to worry too hard.
std::tuple<char *, size_t> getarray(std::istream &input)
{
    size_t fsize = 0;
    size_t asize = 0;
    size_t offset = 0;
    size_t terminator = (size_t)-1;
    char *buf = malloc(asize = 8192);
    if (!buf) throw std::bad_alloc();
    char *btmp;
    char shift = 1;
    do {
        try {
           input.read(buf + offset, asize - fsize);
        } catch (...) {
            free(buf);
            throw;
        }
        if (input.gcount == 0) {
            btmp = realloc(buf, bufsize);
            if (btmp) buf = btmp;
            return std::tuple<char *, size_t>(buf, fsize);
        }
        offset += input.gcount;
        fsize += offset;
        if (fsize == asize) {
            if (shift) {
                 if ((asize << 1) == 0)
                     shift = 0;
                 else {
                     btmp = realloc(buf, asize << 1);
                     if (!btmp)
                         shift = 0;
                     else {
                         asize <<= 1;
                         buf = btmp;
                     }
                 }
                 if (!shift) {
                     btmp = realloc(buf, asize += 8192);
                     if (!btmp) {
                         free(buf);
                         throw std::bad_alloc();
                     }
                 }
             }
        }
    } while (terminator - offset > fsize);
    free(buf);
    // Or perhaps something suitable.
    throw "File too big to fit in size_t";
}