26.8.15

C++ program that uses a single file

Write a C++ program that uses a single file for both reading and writing the data.

#include<iostream.h>

#include<conio.h>

#include<fstream.h> int main()
{

clrscr();

ofstream outf("ITEM.txt"); cout<<"Enter the filename:"; char name[30];

cin>>name;

outf<<name<<"\n"; cout<<"Enter ITEM cost:"; float cost;

cin>>cost;
outf<<cost;
outf.close();

ifstream inf("ITEM.txt"); inf>>name;
inf>>cost;

cout<<"The name of the item is:"<<name; cout<<"\nItem cost is :"<<cost; inf.close();

getch(); return 0;
}

**************************************OUTPUT****************************

Enter the ITEM name:vicks

Enter ITEM cost:20.3
The name of the item is:vicks

Item cost is :20.299999

No comments:

Post a Comment

Comment Here