Saturday 17 September 2011

Read the content from the file.


Read the content from the file.
Aim:
To write a C++ program to read the contents from a file.
Algorithm:
Step 1: Include the header file as fstream.h
Step 2: Declare the variables as rollno, dept, name and year.
Step 3: Declare the filename “stu.txt” using ifstream.
Step 4: Read the values of rollno, dept, name and year from the file.
Step 5: Display the values of rollno, dept, name and year.

Program:
#include<fstream.h>
void main()
{
char dept[20];
char name[20];
char year[10];
int roll;
ifstream inf("stu.txt");
inf>>roll;
inf>>dept;
inf>>name;
inf>>year;
cout<<"\nRoll:"<<roll;
cout<<"\nName:"<<name;
cout<<"\nYear:"<<year;
cout<<"\nDept:"<<dept;

}

Output:

Roll: 101
Name:RAM
Year:II
Dept:IT

0 comments:

Post a Comment