Я пытаюсь создать несколько бинарных деревьев из чтения из одного файла txt. Для этого я должен прочитать каждый символ в строке, создать дерево, а затем перейти к следующей строке, чтобы создать другое дерево. Мой текущий код захватывает ввод строки за строкой, и я не уверен, как это изменить?
Это то, что у меня есть сейчас:
int main()
{
vector <BinaryTree <string> > BT;
int iteration = 0;
string line;
ifstream myfile("input.txt");
if (myfile.is_open())
{
while(getline (myfile, line))
{
BinaryTree <string> temptree;
BT.push_back(temptree);
BT[iteration].InsertData(line);
cout << "Preorder: ";
BT[iteration].PrintPreorder();
cout << endl;
cout << "Inorder: ";
BT[iteration].PrintInorder();
cout << endl;
cout << "Postorder: ";
BT[iteration].PrintPostorder();
cout << endl;
cout << "Reverse Inorder: ";
BT[iteration].PrintReverseInorder();
cout << endl;
BT[iteration].PrintPrintTree();
cout << endl;
iteration++;
myfile.close();
}
return 0;
}
char c;
while (myfile>> std::noskipws >> c)
{
if(c == '\n')
printf("new line\n");
printf("%c\n", c);
}
используйте operator>>
для чтения символа по символу, а затем проверьте, если вы получите разрыв строки, std::noskipws
означает, что начальные пробельные символы являются допустимым содержимым, которое нужно извлечь. Или вы также можете использовать ifstream.get()
следующим образом:
char c;
while (myfile.get(c))
{
if(c == '\n')
printf("new line\n");
printf("%c\n", c);
}
char c = ifs.get()