welcome everybody, i am able read my file by asking the filename from user but i am facing a little problem,When i type a name of file which doesnot exist,compiler also shows it with some zeroes, this is my program, please help me,having problem in case 2...
#include <fstream>
#include <iostream>
#include<stdlib.h>
using namespace std;
char ch;
int main()
{
char name[1000];
char lname[1000];
long Age;
int choice;
char FileName[50]="Filename.txt";
fstream students;
while(1)
{
system("cls");
cout<<"*1*\tCreate a file\t\n";
cout<<"*2\tOpen the File\t\n";
cout<<"*3*\tExit the Programme\t\n";
cout<<"\n\n"<<endl;
cout<<"Enter your choice\n"<<endl;
cin>>choice;
system("cls");
switch(choice)
{
case 1:
{
cout << "Enter First Name: "<<endl;
cin>>name;
cout << "Enter Last Name: "<<endl;
cin>>lname;
cout << "Enter Age : "<<endl;
cin>>Age;
cout << "\nEnter the name of the file you want to create: "<<endl;
cin >> FileName;
ofstream students;
students.open(FileName, ios::app);
students << name << "\n"<< lname << "\n" << Age<<endl;
students.close();
break;
}
case 2:
{
ifstream rstudents;
cout<<"FileName = "<<endl;
cin>>FileName;
rstudents.open(FileName);
{
rstudents>>name;
rstudents>>lname;
rstudents>>Age;
cout << "\nFirst Name: " << name;
cout<<"\nlast name: "<<lname;
cout << "\n Age: " << Age;
}
students.close();
}
case 3:
{
cout<<"\nGood Bye"<<endl;
system("pause");
exit(0);
}
}
}
}