1

I have to copy characters of a file in a big size array, so I created this code:

std::vector<std::vector<char> > strings;

strings.resize(rows);

for (int i = 0; i < rows; i++)  
{ 
    strings[i].resize(columns); 
}

ifstream in("filename.txt");

for (int i = 0; i < rows; i++)    
    in.getline(strings[i], columns);

strings should contain all the characters of the file, but when I compile this program, I have the following error:

no matching function for call to ‘std::basic_ifstream >::getline(std::vector >&, int)’</p>

(and others error lines)

How can I copy all characters of a file in a big char array?

4

3 回答 3

0

正如 OGH 建议的那样,我将使用字符串标题的 getlinestd::vector<std::string> strings;代替vector<vector<char>> strings;和 qork:

getline(in, strings[i]);

于 2013-04-03T13:54:22.313 回答
0

in.getline()不能将向量char作为其第一个参数。你应该使用std::string.

你可以做getline(in, strings[i]);

要填写二维向量的值,您可以执行以下操作:

for (int i = 0; i < rows; i++) {
  for (int j = 0; j < columns; j++) {
    in >> strings[i][j];
  }
}
于 2013-04-03T13:18:27.250 回答
0

你应该使用std::stringinstread ofstd::vector<char>

如果由于某种原因你真的需要使用数据结构std::vector<std::vector<char> >

然后用于std::string读取文件然后将其转换为std::vector<char>但我不明白你为什么要这样做

于 2013-04-03T13:20:18.073 回答