This article shows how to write files using fstream, a library for reading and writing files in C++. It also shows how to add characters to the end of a file.
Example of creating a new file
The following is an example of outputting a string of type string as a text file named sample.txt using a variable ofstream that can handle file writing.
sample code
タイトル:main.cpp
#include <fstream> #include <string> int main(int argc, char *argv[]){ std::ofstream writing_file; std::string filename = "sample.txt"; writing_file.open(filename, std::ios::out); std::string writing_text = "C++ is a general-purpose programming language. "; writing_file << writing_text << std::endl; writing_file.close(); return 0; }
Explanation of Codes
I will explain the code. We will use two libraries, one of which is fstream, which is necessary for writing files, since string is a library necessary for handling strings of type string.
タイトル:main.cpp
std::ofstream writing_file; std::string filename = "sample.txt"; writing_file.open(filename, std::ios::out);
First, a variable ofstream type writing_file
is declared, which is needed to write the file, and a file named sample.txt
is expanded using the open member function. If the filename sample.txt does not exist, a new file is created. the open function specifies the mode in which the file is to be opened as its second argument. This is the mode defined in the std::ios_base
class, where out means write-only. The second argument is optional, and out is specified by default for ofstream (in this case, the second argument is explicitly specified intentionally).
タイトル:main.cpp
std::string writing_text = "C++ is a general-purpose programming language. "; writing_file << writing_text << std::endl;
Next, we add a string of type string to a variable ofstream. Since this is a stream, the string is added using the <<
operator.
タイトル:main.cpp
writing_file.close();
Finally, memory is released by closing the expanding ofstream. This process does not need to be described, as it is done automatically when execution leaves the scope ofstream type, but it is used for the sake of introduction.
Example of writing additionally to a file
While the previous section introduced rewriting the entire contents of a file, this section introduces an additional method of writing to a file that has already been written.
sample code
タイトル:main.cpp
#include <fstream> #include <string> int main(int argc, char *argv[]){ std::ofstream writing_file; std::string filename = "sample.txt"; writing_file.open(filename, std::ios::app); std::string writing_text = "An important principle of language design is to not require low-level languages other than assembly language and to not require time and space costs for functions that are not used."; writing_file << writing_text << std::endl; writing_file.close(); return 0; }
Explanation of Codes
There is only one difference from the previous code. Please refer to the explanation in the previous section for the processing other than the changes.
タイトル:main.cpp
writing_file.open(filename, std::ios::app);
We just specify std::ios::app
to open the file. app stands for append, which means to move the write position to the end of the stream, just before writing. Thus, it is possible to append a string to the end of a read file.
Creating files without ofstream
We have just introduced output using ofstream, but you can also create a file by outputting a file on standard output at file execution. This method is useful for outputting execution logs, for example.
タイトル:terminal
$ ./main.out > sample.txt
By specifying the above at runtime, the standard output of main.out can be output to sample.txt, which is easy because there is no need to install ofstream. It is not possible to generate multiple files at one time, so it is necessary to use it according to your needs.
Summary
This article has described how to write files in C++. The contents are briefly summarized at the end.
- It is possible to write a file by using the ofstream type of the fstream library.
- Appending the end of a file by specifying app as the second argument of the open member function
- Specifying an output file when executing a file also allows outputting a file on the standard output.