我一直在尝试将文本文件的每行存储到C++的列表中。更好的方法是,我一直在尝试将文本文件每行的每个单词存储在字符串列表中的一个字符串中,但似乎我的方法不正确。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <list>
using namespace std;
int main()
{
FILE *f= fopen("teste.txt", "r");
size_t len= 100; // valor arbitrário
char *line= (char*)malloc(len);
std::list<string> mylist;
if (!f)
{
perror("teste.txt");
exit(1);
}
while (getline(&line, &len, f) > 0)
{ //THE REAL PROBLEM
for (std::list<string>::iterator it = mylist.begin(); it != mylist.end(); it++){
*it=line;
cout << *it << '\n';
}
}
if (line)
free(line);
fclose(f);
return 0;
}
确切的问题是这个没有给出任何结果。编译成功了,但是没有任何结果显示。 提前感谢。
*for
:o - deviantfan