如何将字符串复制到指针数组的元素?

时间:2013-06-15 00:59:39

标签: c++ arrays pointers

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

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();
}

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

3 个答案:

答案 0 :(得分:1)

请改用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>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)));

答案 1 :(得分: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自行关闭。向量删除它分配的内容。这是一个更小的更高效的代码,可以将文件行作为字符串。

答案 2 :(得分:-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
    }
}

如果你确定在这个过程中你永远不需要实际更改数组,你可以(and it is prudent to)将向量引用作为常量传递:

void ProcessLinesInFile(std::vector<std::string> const &lines);