I need to read a txt.file in a linked list , but nothing is displayed .
Code:
#include <cstdio> #include <fstream> #include <iostream> using namespace std; struct MyStruct { char name[20]; char surename[20]; }; int main() { int N; cout<<"enter N: "; cin>>N; MyStruct apartment; ofstream f; f.open("C:\\Wext1.txt"); for(int i=0; i<N; i++) { cout<<"enter name: "; cin>>apartment.name; f << apartment.name << " "; cout<<"enter surename: "; cin>>apartment.surename; f << apartment.surename << " "<<endl; } return 0; } #include <iostream> #include<fstream> using namespace std; struct MyStruct { char name[20]; char surename [20]; MyStruct *next; }; void create_list(MyStruct* &h1,ifstream &f) { MyStruct* temp; h1=new MyStruct; temp=h1; f>>temp->name>>temp->surename; while(f.peek()!='\n') { temp->next=new MyStruct; temp=temp->next; temp->next=NULL; f>>temp->name>>temp->surename; } } int main() { ifstream file("C:\\Wext1.txt"); if(!file.is_open()) return -1; MyStruct *list; create_list(list,file); while (list!=NULL) { cout<<list->name<<list->surename<<endl; list=list->next; } file.close(); return 0; }
Comment