Ok, now I'm trying out a very basic program in turbo c / DOSBOX using filestream. What I've noticed is that my ofstream variable works properly while writing data into the file, but the problem is in retrieving the data. The ifstream variable isn't working properly for some reason, or the method used here to retrieve the file data maybe improper. If you can help me out with this, please contact me.
This is my code:-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<fstream.h>
void main()
{
char user[20],pin[10];
ofstream fout("TEMP");
ifstream fin("TEMP");
clrscr();
fin>>user;
cout<<"Username : "<<user;
cout<<"\nEnter username.\n";
gets(user);
cout<<"Enter pin.\n";
cin>>pin;
fout<<"Username : "<<user<<endl<<"Pin : "<<pin;
cout<<"\nUsername : "<<user;
getch();
}
The data I entered is : Username = dragonis. pin = 123.
Everything is stored perfectly in the file. But when retrieving, there is no output of username when cout is given at the beginning of the program. It is blank.
ofstreammight be to create a new empty file. Then there is nothing to read.