0% found this document useful (0 votes)
35 views24 pages

Files

C++ files concept Bca 2nd year

Uploaded by

Hiteshi Arya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
35 views24 pages

Files

C++ files concept Bca 2nd year

Uploaded by

Hiteshi Arya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 24
File VO A file is a bunch of bytes stored on some storage devices like hard disk, floppy disk etc. File /O and console VO are closely related, In fact, the same class hierarchy that supports console 1/0 also supports the file /O. To perform file /O, you must include in your program. It defines several classes, including ifstream, ofstream and fstream. In C++, a file is opened by linking it to a stream, There are three types of streams: input, output and inpuoutput. Before you can open a file, you must first obtain a stream. OS Iponren Shea ploua ¥ date. or find of chasing, + Styuavas as Used for a.c0ssiing, as data foun ousicls aaa progr ~promn extra soda sever aserarentical ol Aueect tetany stl clan —— ee fon aeasiing st sou, Jor. _giting 0 Aunciingy — iain data, We une the _mnecherisira — %. _ Shean ——— ces Hivnaetay ine + on ; - —4_dintan filo — 3: Aeprusuntation int 2233. Ryphuwnbacion int _1223— a 4 ————4_____ 0000000000 fo) 2 ot (nscuitocte). oe — = eee neni —setioele |—20/1o0 11 = 4s Text file are ato Binensy felis are foster i site pits ad ear ere sich for dusiguing of dato to trast —0f pile 7s fan_straanappor fits, —__ 2 No formatting —dia-dowe —__8._fommaathiing din douse la Cover_mon space im ___4._Doaanet_couta mare I ae aan ree Sma ey Text Files: e Limitation: Inefficient for storing non-textual data. ¢ Solution: Use ~ifstream* and “ofstream* for reading and writing, and libraries like Boost.lIOStreams for advanced operations. Binary Files: ¢ Limitation: Less human-readable and harder to debug. ¢ Solution: Serialize structured data, use libraries like Protocol Buffers or JSON, and handle byte order with libraries like Boost.Endian. Operation OpenaFile Read from a File Write toa File Close a File Modify File Content Seek within a File Truncate a File Delete a File Description Open an existing file or create a new one with specified access modes. Read data from an opened file into memory. Write data from memory to an opened file. Release system resources associated with an opened file. Change the data within an opened file. Move the file pointer to a specific position within the file. Shorten the size of an opened file. Remove a file from the file system. How to opena file in C++ + To read or enter data to afile, we need to open it first. + This can be performed with the help of ‘ifstream’ for reading and ‘ftream’ or ‘ofstream’ ‘for writing or appending to the file. q N. All these three objects have open() function that is inbuilt in them. Syntax: open( FileName , Mode ); e Example:- > open(“sksfile”, ios:out); 3b a Here: FileName — It denotes the name of file which has to be opened. Mode — There are different modes to opena file. Mode Description ioszin File opened in reading mode ioss:out File opened in write mode iosapp File opened in append mode ioszate File opened in append mode but read and write performed atthe end of the file. ios:binary File opened in binary mode ios:trune _| File opened in truncate mode ios:mocreate _ | The file opens only ifit exists ios:moreplace The file opens only if it doesn’t exist ¢@ NOTE In C+, we can use two modes simultaneously with the help of OR (|) operator. Example:- open(‘“sksfile”, ios:out|ios:in); i ba Program for Opening File: #include #include void main() { fstream FileName; FileName.open(“sksfile”, ios::out); cout<<"File created successfully"; FileName.close(); } How to close a file in C++ « When a C++ program terminates, it automatically flushes all the streams, release all the allocated memory and close all the opened files. e Butitis always a good practice that a programmer should close all the opened files before program termination. e Wecan close an opened. file by using close() function. @ Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects:- void close(); Program for Closing File: #include #include void main() { fstream FileName; FileName.open(‘sksftle”, ios::out); cout<<"File created successfully"; FileName.close(); } In C++ we have mainly two pointers i.e. a get pointer and a put pointer for getting (ie. reading) data from a file and putting (Le. writing) data on the q file respectively. These pointers help to access the file ina random manner. By using these pointers we can perform both reading and writing operations randomly in a file. Hb Functions associated with File Pointers- seekg() ily » seen) | a eh tellp() f. ». A | seekg() is used to move the get pointer to a desired location with respect to a reference point. Syntax: file : pointer.seekg (number of bytes Reference point) ; Example: fin.seekg(io,ios::beg); tellg( ):- tellg() is used to know where the get pointer is in a file. Syntax: file pointer tellg(); Example: int posn = fin.tellg(); see! as seelep() is used to move the put pointer to a desired location with respect to a reference point. Syntax: file pointer.seekp(number of bytes Reference point); Example: fout.seekp(i0,ios::beg); tellpQ) is used to know where the put pointer is in a file. Syntax: file pointer -tellp(Q); Example: int posn=fout.tellp(); See! ae seelep() is used to move the put pointer to a desired location with respect to a reference point. Syntax: file pointer.seekp(number of bytes Reference point); Example: fout.seekp(io,ios::beg); The reference points are: ios::beg — from beginning of file ioszend — from end of file ios::cur — from current position in the file.

You might also like