Sunday 25 September 2011


Write into the file.
Aim:
To write a C++ program to write the contents into 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: Create a filename “stu.txt” using ofstream.
Step 4: Read the values for rollno, dept, name and year.
Step 5: Write the values of rollno, dept, name and year into the file.
Program:
#include<fstream.h>
void main()
{
int roll;
char dept[10];
char name[45];
char year[20];
ofstream out("stu.txt");
cout<<"enter the roll:";
cin>>roll;
cout<<"Enter the name:";
cin>>name;
cout<<"Enter the year:";
cin>>year;
cout<<"enter the dept:";
cin>>dept;
out<<roll;
out<<name;
out<<year;
out<<dept;
}

Output:

STU.TXT
101
RAM
II
IT

0 comments:

Post a Comment