我有一个文件,我需要在程序开始运行时读取,然后我需要将每个3个字符放入双链表中的节点中。我开始从文件中读取,但我不能将每个元素作为char并放入双链表中。我写了这个,但我卡住了。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
int i;
string myText;
ifstream MyReadFile("example.txt");
while(MyReadFile>>noskipws>>myText){
cout<<myText
}
MyReadFile.close();
return 0;
}
下面是一些帮助您入门的代码:
struct Three_chars
{
char a, b, c;
};
char a;
char b;
char c;
std::list<Three_chars> database;
while (MyReadFile >> noskipws >> a >> b >> c)
{
Three_chars triple;
triple.a = a;
triple.b = b;
triple.c = c;
database.push_back(triple);
}
上面的代码将Three_chars
声明为包含三个字符的节点。while
表达式将3个字符读入字符变量。while
循环创建一个节点,将读取的3个字符复制到节点,然后将该节点附加到链表中。