#include
#include
#include
using namespace std;
int main()
{char filename[30];
int num,i;
string name,classs,line;
ifstream input;
ofstream output;
cout<<"what is the name of the file you are using? ";
cin>>filename;
output.open(filename); //open file
if(output.fail())
{ cout<<"file did not open please check it ";
system("pause");
return 1;
}
output<<"Roll NO Student Name Class ";
cout<<"Enter Roll No ( <0 to exit): ";
cin>>num;
while(num>0)
{cin.ignore(255,' ');
cout<<"Enter name: ";
getline(cin,name);
cout<<"Enter class: ";
getline(cin,classs);
output<<" "<<<" "<<<" "<<
cout<<"Enter Roll No ( <0 to exit): ";
cin>>num;
}
output.close();
input.open(filename); //open file
if(input.fail())
{ cout<<"file did not open please check it ";
system("pause");
return 1;
}
cout<
while(input)
{getline(input,line);
cout<<
}
output.close();
system("pause");
return 0;
}
______________________________
2nd SOLUTION
______________________________
#include
#include
#include
int main(void)
{
char rollNo[10],studentName[20],studentClass[20];
ifstream iFile;
ofstream oFile;
oFile.open("Student_info.txt",ios::out);
iFile.open("Student_info.txt");
if (!iFile)
{
cout << "iFile could not be opend";
}
if (!oFile)
{
cout << "oFile could not be opened";
}
oFile << "RollNo" << "\t" << "StudentName" << "\t" << "Class" <
oFile << "101" << "\t" << "AliRaza" << "\t" << "BS(CS)" <
oFile << "102" << "\t" << "Usman" << "\t" << "MS(CS)" <
oFile << "103" << "\t" << "Faizan" << "\t" << "BS(CS)" <
oFile << "104" << "\t" << "Rehan" << "\t" << "MIT" <
oFile << "105" << "\t" << "Ahmed" << "\t" << "MCS";
oFile.close();
while (!iFile.eof())
{
iFile >> rollNo >> studentName >> studentClass;
cout << rollNo << "\t"<< studentName << "\t"<<"\t"<
}
iFile.close();
system("PAUSE");
return 0;
}
www.VUsolutions.blogspot.com
Problem Statement:
Writing and Reading student data in a file.
Detailed Description:
Write a program in which you have to:
1. Create a text file “Student_info”.
2. Using Code Write the following data in it:
Roll NO Student Name Class
101 Ali Raza BS(CS)
102 Usman MS(CS)
103 Faizan BS(CS)
104 Rehan MIT
105 Ahmed MCS
3. Open file and read data from it and display on the screen.
Sample Output
Student Information
Roll NO Student Name Class
101 Ali Raza BS(CS)
102 Usman MS(CS)
103 Faizan BS(CS)
104 Rehan MIT
105 Ahmed MCS
Deadline
Your assignment must be uploaded/submitted on or before 18-06-2009.
0 comments:
Post a Comment