In my program i am storing data given by the user in a .txt file with the help of ofstream. I am using setw() to ensure proper gaping between the entered data and i am storing the data of a user in a single row. Here is the code for that:
string n,mn,fn;
int a;
cout<<"Enter your full name: ";
getline(cin, n);
cin.ignore();
cout<<"Father name: ";
getline(cin, fn);
cin.ignore();
cout<<"Mother name: ";
getline(cin, mn);
cin.ignore();
cout<<"Enter your age: ";
cin>>a;
ofstream file("my.txt",ios::app);
file<<left<<setw(50)<<n<<setw(50)<<a<<setw(50)<<fn<<setw(50)<<mn<<endl;
file.close();
My .txt file saves data properly with proper spacing as:
parth kumar 12 jack worn juli zeel
standly duke 19 shane roger zoya khan
now I want to ifstream this data in my same program. I wrote the following code to ifstream only first row. Here is the code:
string fn,mn,n;
int a;
ifstream file("my.txt");
file>>left>>setw(50)>>n;
file>>setw(50)>>a;
file>>setw(50)>>fn;
file>>setw(50)>>mn
cout<<n<<" "<<a<<" "<<fn<<" "<<mn;
This code is not giving desired results. I am just trying to ifstream 1st row contents. is it posssible? Or i should store each set of data in different file (like names in one, ages in one, father names in one, mother names in other). Can you help?
Aucun commentaire:
Enregistrer un commentaire