3. What are different file types? Give their functions with an example for each.
Answers
Answered by
0
TEXT FILE. It is a file that stores information in ASCII characters. In text files, each line of text is terminated with a special character known as EOL (End of Line) character or delimiter character. When this EOL character is read or written, certain internal translations take place.
BINARY FILE. It is a file that contains information in the same format as it is held in memory. In binary files, no delimiters are used for a line and no translations occur here.
Classes for file stream operation
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.
Opening a file
OPENING FILE USING CONSTRUCTOR
ofstream outFile("sample.txt"); //output only
ifstream inFile(“sample.txt”); //input only
OPENING FILE USING open()
Stream-object.open(“filename”, mode)
ofstream outFile;
outFile.open("sample.txt");
ifstream inFile;
inFile.open("sample.txt");
For example, if we want to open the file example.bin in binary mode to add data we could do it by the following call to
member function open():
fstream file;
file.open ("example.bin", ios::out | ios::app | ios::binary);
Closing File
outFile.close();
inFile.close();
INPUT AND OUTPUT OPERATION
put() and get() function
the function put() writes a single character to the associated stream. Similarly, the function get() reads a single character form the associated stream.
example :
file.get(ch);
file.put(ch);
write() and read() function
write() and read() functions write and read blocks of binary data.
example:
file.read((char *)&obj, sizeof(obj));
file.write((char *)&obj, sizeof(obj));
ERROR HANDLING FUNCTION
FUNCTIONRETURN VALUE AND MEANINGeof()returns true (non zero) if end of file is encountered while reading; otherwise return false(zero)fail()return true when an input or output operation has failedbad()returns true if an invalid operation is attempted or any unrecoverable error has occurred.good()returns true if no error has occurred.
BINARY FILE. It is a file that contains information in the same format as it is held in memory. In binary files, no delimiters are used for a line and no translations occur here.
Classes for file stream operation
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.
Opening a file
OPENING FILE USING CONSTRUCTOR
ofstream outFile("sample.txt"); //output only
ifstream inFile(“sample.txt”); //input only
OPENING FILE USING open()
Stream-object.open(“filename”, mode)
ofstream outFile;
outFile.open("sample.txt");
ifstream inFile;
inFile.open("sample.txt");
For example, if we want to open the file example.bin in binary mode to add data we could do it by the following call to
member function open():
fstream file;
file.open ("example.bin", ios::out | ios::app | ios::binary);
Closing File
outFile.close();
inFile.close();
INPUT AND OUTPUT OPERATION
put() and get() function
the function put() writes a single character to the associated stream. Similarly, the function get() reads a single character form the associated stream.
example :
file.get(ch);
file.put(ch);
write() and read() function
write() and read() functions write and read blocks of binary data.
example:
file.read((char *)&obj, sizeof(obj));
file.write((char *)&obj, sizeof(obj));
ERROR HANDLING FUNCTION
FUNCTIONRETURN VALUE AND MEANINGeof()returns true (non zero) if end of file is encountered while reading; otherwise return false(zero)fail()return true when an input or output operation has failedbad()returns true if an invalid operation is attempted or any unrecoverable error has occurred.good()returns true if no error has occurred.
Similar questions
Physics,
6 months ago
Math,
6 months ago
Business Studies,
1 year ago
Physics,
1 year ago
Math,
1 year ago