Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

Learn C++ Language

Fstream Library open() Function in C++ Programming Language

Fstream Library open() Function in C++
Open file. Opens the file identified by argument filename, associating it with the stream object, so that input/output operations are performed on its content. Argument mode specifies the opening mode. If the stream is already associated with a file (i.e., it is already open), calling this function fails. The file association of a stream is kept by its internal stream buffer: Internally, the function calls rdbuf()->open(filename,mode|ios_base::out) The function clears the stream's state flags on success (setting them to goodbit). In case of failure, failbit is set.
Syntax for Fstream open() Function in C++
void open (const char* filename, ios_base::openmode mode = ios_base::in | ios_base::out); void open (const string& filename, ios_base::openmode mode = ios_base::in | ios_base::out);
filename
String with the name of the file to open. Specifics about its format and validity depend on the library implementation and running environment.
mode
Flags describing the requested input/output mode for the file. This is an object of the bitmask member type openmode that consists of a combination of the following member constants: • in input File open for reading: the internal stream buffer supports input operations. • out output File open for writing: the internal stream buffer supports output operations. • binary binary Operations are performed in binary mode rather than text. • ate at end The output position starts at the end of the file. • app append All output operations happen at the end of the file, appending to its existing contents. • trunc truncate Any contents that existed in the file before it is open are discarded. These flags can be combined with the bitwise OR operator (|). If the mode has both trunc and app set, the opening operation fails. It also fails if trunc is set but out is not. This function does not return any value. If the function fails to open a file, the failbit state flag is set for the stream (which may throw ios_base::failure if that state flag was registered using member exceptions).
Data races
Modifies the fstream object. Concurrent access to the same stream object introduce data races.
Exception safety
Basic guarantee: if an exception is thrown, the stream is in a valid state. It throws an exception of member type failure if the function fails (setting the failbit state flag) and member exceptions was set to throw for that state.
/* Fstream library open() function opens the file identified by argument filename, associating it with the stream object, so that input/output operations are performed on its content. */ #include <iostream> #include <fstream> using namespace std; int main() { fstream new_file; new_file.open("new_file_write.txt",ios::out); if(!new_file) { cout<<"File creation failed"; } else { cout<<"New file created"; new_file<<"Learning File handling"; //Writing to file new_file.close(); } return 0; }