![]() The main difference between a stream object and a DOS file is that streams are not opened using a file handle, but through an IStream interface pointer. For example, each stream object has its own access rights and a seek pointer. The IStream interface defines methods similar to the MS-DOS FAT file functions. Simple data can be written directly to a stream, but most frequently, streams are elements nested within a storage object. Stream objects contain the data in a structured storage object, where storages provide the structure. St.seekg(1, ios::cur) //Go to position 1 from beginning.Ĭout<<"As per tellg File Pointer Position is "<<st.The IStream interface supports reading and writing data to stream objects. St.seekg(5, ios::beg) // Go to position 5 from begning.Ĭout<<"As per tellg File Pointer Position is "<<st.tellg()<<endl //Checking file pointer position St.open("E:\studytonight.txt",ios::in) // Opening file in read mode St.seekp(-1, ios::cur) // Go one position back from current positionĬout<<"As per tellp File Pointer Position is "<<st.tellp()<<endl St.open("E:\studytonight.txt",ios::out) // Creating new fileĬout<<"File Pointer Position is "<<st.tellp()<<endl Note: For seekp and seekg three reference points are passed:īelow is a program to show importance of tellp, tellg, seekp and seekg: #include įstream st // Creating object of fstream class get() - It reads a single character from file.put() - It writes a single character to file.Syntax: ekg((no of bytes,reference point) seekg() - It moves get pointer(input) to a specified location.seekp() - It moves the put pointer to mentioned location.tellg() - It tells the current position of the get pointer.tellp() - It tells the current position of the put pointer.There are few important functions to be used with file streams like: St.open("E:\studytonight.txt",ios::in) // Step 2: Creating new file If(!st) // Step 3: Checking whether file existįstream st // step 1: Creating object of fstream class St.open("E:\studytonight.txt",ios::out) // Step 2: Creating new file Example of file opened for truncating: st.open("E:\studytonight.txt",ios::trunc) #includeįstream st // Step 1: Creating object of fstream class.Example of file opened for appending: st.open("E:\studytonight.txt",ios::app). ![]() Example of file opened for reading: st.open("E:\studytonight.txt",ios::in).Example of file opened for writing: st.open("E:\studytonight.txt",ios::out).Syntax for file creation: FilePointer.open("Path",ios::mode) Operations can be reading, writing, appending and truncating. ![]() ![]() We create/open a file by specifying new path of the file and mode of operation. So it can read from files and write to files. fstream: It represents both output Stream and input Stream.ifstream: It represents input Stream and this is used for reading from files.ofstream: It represents output Stream and this is used for writing in files.So we use the term File Streams/File handling. ![]() text or binary data permanently and use these data to read or write in the form of input output operations by transferring bytes of data.
0 Comments
Leave a Reply. |