File Input Output C++
In C/C++ programming language, Input/output library refers to a family of class templates and supporting functions in the C++ Standard Library that implement stream-based input/output capabilities. Some Useful Functions that might come very handy in I/O.
I/O libraries
ifstream: Stream class to read. ofstream: Stream class to write. fstream: Stream class to read and write.
ifstream OpenFile(char *filename, int mode);
- You should know the the file name which is stored in the filename var and the mode determine how to open the file.
- ios::in Open file to read ios::
- out Open file to write
- ios::app All the date you write, is put at the end of the file. It calls ios::
- out ios::ate All the date you write, is put at the end of the file. It does not call ios::out
- ios::trunc Deletes all previous content in the file. (empties the file)
- ios::nocreate If the file does not exists, opening it with the open() function gets impossible.
- ios::noreplace If the file exists, trying to open it with the open() function, returns an error.
- ios::binary Opens the file in binary mode.
Create file and input some string C++
#include <fstream> using namespace std; int main() { ofstream myfile; myfile.open("mycodinglab.txt"); myfile << "writing this to mycodinglab.txt \n"; myfile.close(); return 0; }
Check is a file is open while reading I/O C++
ofstream myfile("mycodinglab.txt"); if (myfile_open()) or if (!myfile.eof()) state flags 1 bad() Returns true if a reading or writing fails. fail() Returns true if a format error occure. eof() Returns true if a file reading is at the end. good() Returns true if there are no errors.
Reading File Input/Output C++ source code
#include <fstream.h> void main() { ifstream myfile; myfile.open("mycodinglab.txt"); char out; while(!myfile.eof()) { myfile.get(ch); cout << out; } my_file.close(); return 0; } //Closing a file myfile.close();
For Example
#include <fstream.h> void main() { ofstream SaveFile("mycodinglab.txt", ios::ate); SaveFile << "That's new!\n"; SaveFile.close(); }
Some more Input/Output C++ source code
Example 1: The most usual way
fstream File(“cpp-home.txt”); if (!File) { cout << “Error opening the file! Aborting…\n”; exit(1); }
Example 2: If the file is created, return an error
ofstream File("unexisting.txt", ios::nocreate); if(!File) { cout << “Error opening the file! Aborting…\n”; exit(1); }
Example 3: Using the fail() function
ofstream File("filer.txt", ios::nocreate); if(File.fail()) { cout << “Error opening the file! Aborting…\n”; exit(1); }
Functions that might come very handy in Input/Output C++
- tellg() – Retunrs an int type, that shows the current position of the inside-pointer. This one works only when you read a file.
- tellp() – The same as tellg() but used when we write in a file. To summarize: when we read a file, and we want to get the current position of the inside-pointer, we should use tellg(). When we write in a file, and we want to get the current position of the inside-pointer, we should use tellp().
- seekp() – go to specified position.
- ignore() – Used when reading a file. If you want to ignore certain amount of characters, just use this function.