0

我有一个指向字符串类的指针数组,我需要将文件中的一行复制到每个指针中,但我不知道该怎么做。

void Document::loadFile(string iFileExt){
  ioFile = new fstream(iFileExt.c_str(), ios::in);
  int i = 0;
  string row;
  string *content;

  if (ioFile->fail()){
    cerr << "File failed to open for read" << endl;
    exit(69);
  }

  while(ioFile->good()){ // this loop is just to know how may rows are in the file
    getline (*ioFile, row); 
    i++;
  }

  content = new string[i]; // I allocate memory dynamically so that the numbers of   
  ioFile->seekg(0);        // pointer is the same as the number of rows   
  i = 0;

  while(ioFile->good()){
    getline (*ioFile, *content[i]);  //this is the tricky part
    i++;
  }
  ioFile->close();
}

提前感谢您可以提供给我的任何帮助或提示!:-)

4

3 回答 3

1

改用 a deque<string>。您将避免很多痛苦,包括需要两次读取文件:

void Document::loadFile(string iFileExt){
    if (ifstream fin(iFileExt)) {
        string row;
        deque<string> content;

        while(getline(fin, row))
            content.push_back(row);

        // Do something with content.
    } else {
        cerr << "File failed to open for read" << endl;
        exit(69);
    }
}

注意:在 C++11 中——由于移动语义——使用vector<string>and可能会更好push_back(move(row)),尽管 I/O 可能会淹没差异。

编辑:如果你真的需要指向字符串的指针,上述方法同样适用于微小的变化:

        deque<string *> content;

        while(getline(fin, row))
            content.push_back(new string(row));

然而,这是一个坏主意。至少,您应该使用智能指针。在 C++11 中,这很容易:

        vector<shared_ptr<string>> content;

        while(getline(fin, row))
            content.push_back(make_shared<string>(move(row)));
于 2013-06-15T01:07:22.083 回答
1

为什么你的不起作用:

getline (*ioFile, *content[i]);  //this is the tricky part
                 ^^^
// You have an extra dereference above

它应该只是:

getline (*ioFile, content[i]);

你应该怎么做:

std::ifstream f(filename);
std::vector<std::string> lines;
for(std::string temp; std::getline(f, temp); lines.push_back(std::move(temp)));

注意:这里不需要清理。ifstream 会自行关闭。向量删除它分配的内容。这是将文件的行作为字符串获取的更小更有效的代码。

于 2013-06-15T01:12:51.937 回答
-1
void Document::loadFile(string iFileExt)
{
  ioFile = new fstream(iFileExt.c_str(), ios::in); // does this ever get deleted?
  // string *content; // arrays have their place, and this is not it
  std::vector<string> content;

  if (ioFile->fail()){
    cerr << "File failed to open for read" << endl;
    exit(69); // don't ever just unconditionally exit from random places
              // it makes for unmaintainable spaghetti code
  }

  for (int i = 0; ioFile->good(); ++i) // loop manages i for us
  {
    content.push_back((string()));
    getline (*ioFile, content[i]);
  }

  ioFile->close();
}

在将使用指针数组的现有代码替换为采用向量的代码时,以下内容可能很有用。

该函数可能具有如下签名:

void ProcessLinesInFile(int numLines, std::string *lines[]); // maybe this
void ProcessLinesInFile(int numLines, std::string **lines);  // or maybe this

两种方式本质上是等价的。 ProcessLinesInFile可能有这样的身体:

for (int i = 0; i < numLines; ++i)
{
    *lines[i] = Process(*lines[i]); // read and maybe write the line
}

第一步是让它使用字符串数组,而不是字符串指针数组:

void ProcessLinesInFile(int numLines, std::string lines[])
{
    // should behave exactly the same way as before
    for (int i = 0; i < numLines; ++i)
    {
        lines[i] = Process(lines[i]); // read and maybe write the line
    }
}

从那里开始,使用向量很容易:

void ProcessLinesInFile(std::vector<std::string> &lines)
{
    // should behave exactly the same way as before
    for (int i = 0; i < lines.size(); ++i)
    {
        lines[i] = Process(lines[i]); // read and maybe write the line
    }
}

如果您确定在整个过程中不需要实际更改数组,您可以(并且谨慎地)将向量引用作为常量传递:

void ProcessLinesInFile(std::vector<std::string> const &lines);
于 2013-06-15T01:08:18.183 回答