【问题标题】:Reading from text file to char* array从文本文件读取到 char* 数组
【发布时间】:2015-07-07 01:48:07
【问题描述】:

如何使用下面的函数将文本文件中的输入输入到 char* 数组? 非常感谢。

char* data2[] = { 0 };

char ch;
fstream fin("../level_15.txt", fstream::in);
while (fin >> noskipws >> ch) {
    cout << ch ; // Or whatever
}

我正在尝试获取如下结构的数组:

char* data[] = {
                /// level 1
                "aaaaaaaaaaaaaa",
                "bbbbbbbbbbbbbb"
            };



cat example.txt
"aaaaaaaaaaaaaa",
"bbbbbbbbbbbbbb"

【问题讨论】:

  • 明显是C++,因为他用fstream
  • 您应该使用std::vector&lt;std::string&gt;&gt; 而不是char* data[];
  • 这个问题没有显示任何研究工作。

标签: c++ arrays char


【解决方案1】:
  std::ifstream fin("source.txt");
  // get pointer to associated buffer object
  std::filebuf* pbuf = fin.rdbuf();
  // get file size using buffer's members
  std::size_t size = pbuf->pubseekoff (0,fin.end,fin.in);
  pbuf->pubseekpos (0,fin.in);
  // allocate memory to contain file data
  char* buffer=new char[size];
  // get file data
  pbuf->sgetn (buffer,size);
  fin.close();

看到 char* 缓冲区包含你想要的。请注意,您应该在使用后释放创建的 char *。

【讨论】:

  • 谢谢,这似乎是一维数组。我可以制作二维数组吗,像这样:“aaaaaaaaaaaaaa”、“bbbbbbbbbbbbbbbb”而不是“aaaaaaaaaaaaaa”bbbbbbbbbbbbbb
  • @hackForLife 新行可以识别为 '\n'。你遍历 char* 并逐行输入你的二维数组。
【解决方案2】:

代码下来,Link for more information and desc

#include <iostream> // library that contain basic input/output functions
    #include <fstream>  // library that contains file input/output functions
    using namespace std;

    int main()
    {
      int array_size = 1024; // define the size of character array
        char * array = new char[array_size]; // allocating an array of 1kb
        int position = 0; //this will be used incremently to fill characters in the array 

        ifstream fin("test.txt"); //opening an input stream for file test.txt
        /*checking whether file could be opened or not. If file does not exist or don't have read permissions, file
      stream could not be opened.*/
      if(fin.is_open())
        {
        //file opened successfully so we are here
        cout << "File Opened successfully!!!. Reading data from file into array" << endl;
        //this loop run until end of file (eof) does not occur
            while(!fin.eof() && position < array_size)
            {
                fin.get(array[position]); //reading one character from file to array
                position++;
            }
            array[position-1] = '\0'; //placing character array terminating character

        cout << "Displaying Array..." << endl << endl;
        //this loop display all the charaters in array till \0 
            for(int i = 0; array[i] != '\0'; i++)
            {
                cout << array[i];
            }
        }
        else //file could not be opened
        {
            cout << "File could not be opened." << endl;
        }
        return 0;
    }

【讨论】:

  • 即使分配的内存会被自动释放,在进程结束后,你应该申请delete []作为new []的对应部分。
  • 我没有看到它实现的问题。你知道怎么做吗?
  • 您的代码存在更多问题,例如while(!fin.eof().
  • Oke 很抱歉,如果你能把部分代码帮助你使用的话。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-23
相关资源
最近更新 更多