0% found this document useful (0 votes)
4 views

T1_U5_File Handling

The document discusses file handling in C++, explaining how to store data permanently using secondary storage devices and files. It outlines the use of input and output streams, including ifstream, ofstream, and fstream for file operations, and details the general steps for file I/O. Additionally, it provides example programs for writing in write and append modes, as well as reading from a file.

Uploaded by

joyalprincess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

T1_U5_File Handling

The document discusses file handling in C++, explaining how to store data permanently using secondary storage devices and files. It outlines the use of input and output streams, including ifstream, ofstream, and fstream for file operations, and details the general steps for file I/O. Additionally, it provides example programs for writing in write and append modes, as well as reading from a file.

Uploaded by

joyalprincess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

FILE HANDLING

· All programs we looked earlier

1. Input data from the keyboard

2. output data to the screen

· Output would be lost as soon as we exist from the program

How do we store data permantely?

· We can use secondary storage devices.

· Data is packaged up on the storage device as data structures called files.

Stream Usage

· we have used streams already

3. cin>> --> input from stream object connected to keyboard.

4. cout<< --> Output to stream object conneted to screen.

5. Can define other streams

· To or from files

· Used similarley as cin , cout

File Input and Output Stream

File Input Stream

reads data from disk file to the program.


File Output Stream

Writes data to the disk file from the program

I/O system of C++ contains

6. ifstream : provides input operations on files

7. ofstream: provides output operation on files

8. fstream: supports for simultaneous input and output operations files.

General File I/O steps

9. Declare a file name variables

10. associate the file name variable with the disk file name

11. open the file

12. use the file

13. close the files

File Stream will be in Memory

C++ Views file as sequence of bytes Ends with end of file marker

File Modes

1. Write mode ( It only write the data into file , it override the previousely stored data into as file)

2.Append mode( It read & Write data into a file )

3. Read mode ( Read data from the file only)

Program-1 : Write the program for Write mode

#include<iostream.h>

#include<fstream.h>

#include<conio.h>
void main()

clrscr();

int pid;

char pname[20];

float rate;

cout<<"\n Enter PID and product name and Rate:";

cin>>pid>>pname>>rate;

ofstream ob;

ob.open("inventory.txt");

ob<<pid;

ob<<"\t";

ob<<pname;

ob<<"\t";

ob<<rate;

ob<<"\n";

cout<<"\n Data stored in to file successfully";

getch();

}
To Check the file location

press window+E ( PC) -->local disk(C)--> TurboC3--> Bin--> inventory.txt

Program-2 : write a program for Append mode

#include<iostream.h>

#include<fstream.h>
#include<conio.h>

void main()

clrscr();

int pid;

char pname[20];

float rate;

cout<<"\n Enter PID and product name and Rate:";

cin>>pid>>pname>>rate;

ofstream ob;

ob.open("inventory.txt",ios::app);

ob<<pid;

ob<<"\t";

ob<<pname;

ob<<"\t";

ob<<rate;

ob<<"\n";

cout<<"\n Data stored in to file successfully";

getch();

}
Program-3 : write a program for read mode

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<stdio.h>

#include<string.h>

void main()

{
clrscr();

fstream fp;

char fname[30],str[100];

int i=0;

cout<<"\n Enter file name to open";

gets(fname);

fp.open(fname,fstream::in);

fp.getline(str,1000);

while(strlen(str>0))

cout<<str<<"\n";

fp.getline(str,1000);

fp.close();

getch();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy