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

T2_U5_Sequential_Read_Write

The document discusses sequential read/write operations in C++, detailing how files can be accessed sequentially or randomly. It explains the use of functions like put() and get() for reading and writing single characters, as well as read() and write() for handling blocks of binary data. Additionally, it includes sample programs demonstrating how to write and read strings and binary data to and from files.

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)
5 views

T2_U5_Sequential_Read_Write

The document discusses sequential read/write operations in C++, detailing how files can be accessed sequentially or randomly. It explains the use of functions like put() and get() for reading and writing single characters, as well as read() and write() for handling blocks of binary data. Additionally, it includes sample programs demonstrating how to write and read strings and binary data to and from files.

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/ 5

Sequential Read/Write operations

· C++ allows the file manipulation command to access the file sequentially or randomly.

· The data of the sequential file should be accessed sequentially , that is , one character at a time.

· In order to access the nth number of bytes, all previous characters are read and ignored.

· There are number of functions to perform read and write opearations with the files.

· Some functions read/write single characters and some functions read/write a single characters,
and some functions read/write blocks of binary data.

· The put() and get() functions are used to read or write a single character , whereas write() and
read() are used to read or write blocks of binary data.

put() - function

· The function put() to writes a character to the specified file by the stream object.

· It is alos member of the fstream class.

· The put() function places a character in the file indicated by the put pointer.

get() function

· The function get() is a member function of the classs fstream.

· This function reads a single character from the file pointed by the get pointer, that is , the
character at the current get pointer position is caught by the get() function

Write a program to write and read string to the file using put() and get() functions

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<stdio.h>

#include<string.h>

void main()

clrscr();

fstream fp;
char str[100];

int i=0;

char ch;

cout<<"\n Enter A Text:";

cin.getline(str,50);

fp.open("krish.txt",ios::in | ios::out);

while(i[str]!='\0')

fp.put(str[i++]);

fp.seekg(0);

cout<<"\n Entered Text :";

while(fp)

fp.get(ch);

cout<<ch;

fp.close();

getch();

}
read() and write() opeartors

· The read() and write() functions perform read and write operations in a binary format that is
exactly the same as an internal representation of data in the computer.

· Due to the capabilities of these functions , large data can be stored in a small amount of
memory.

· Both these functions are also used to write and read class object to and from files.

Write a program for binary file using write()

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

void main()

clrscr();

int num[]={100,120,130,140,150,160};

ofstream out;

out.open("one.bin");
out.write((char *) &num,sizeof(num));

out.close();

cout<<"\n file writed as binary using write()";

getch();

Program to read a character from binary file using read()

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

void main()

clrscr();

int num[90];

ifstream in;

in.open("one.bin");

in.read((char *) &num,sizeof(num));

for(int i=0;i<6;i++)

cout<<"\n"<<num[i];

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