In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-10-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use std::fstream to deal with files in C++. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
1. Use open () and close () to open and close files
(1) to use the fstream class, you need to use open () to open the file
Fstream myFile; / / instantiate a file object myFile.open ("firstFile.txt", ios_base::in | ios_base::out | ios_base::trunc); / / Open the file firstFile.txt. You can choose three modes: if (mfFile.is_open ()) / / check whether open () is successful {Dosomething (); / / read or write myFile.close () / / Don't forget to close the file stream to save its contents.
Open ("path and name of file name", ios_base::in | ios_base::out | ios_base::trunc)
The first parameter: the path and name of the file to be opened (if no path is provided, the default is the current directory of the application)
The second parameter: is the open mode of the file. Ios_base::in (readable file), ios_base::out (writable file), ios_base::trunc (recreate the specified file even if it exists)
(2) in addition to using open () to open a file, you can also use the constructor of the fstream class to open a file
/ use the constructor to open the file fstream myFile ("firstFile.txt", ios_base::in | ios_base::out | ios_base::trunc); / / if you only want to open the file to write ofstream myFile ("firstFile.txt", ios_base::out); / / if you only want to open the file to read ifstream myFile ("firstFile.txt", ios_base::in)
Note: whether you use the member method open () or the constructor to open a file stream, it is recommended that you use is_open () to check whether the file open operation was successful before using the file stream object.
two。 Create a text file using open () and use the operator
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.