[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
userpage » jumper » Announcements » rs 485 » JPEG » DOS » MSDOS » PCI BUS » VBDOS » CppDataType » CppFileIo
You could also use the Boost's serialization library.
Testing this, with the code below, for 100 minutes, however, does not result in an error.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
userpage » jumper » Announcements » rs 485 » JPEG » DOS » MSDOS » PCI BUS » VBDOS » CppDataType » CppFileIo
(C++) File input/output
The reading from and writing to a file. Aften abbreviated to File I/O.Example #1
void writeToFile(const std::string& fileName, const double& value) { std::ofstream myFile(fileName.c_str()); assert(myFile.is_open()==true); myFile << "bilderbikkelWasHere" << '\n' << value; myFile.close(); } double readFromFile(const std::string& fileName) { std::ifstream myFile(fileName.c_str()); std::string myData = "Nothing yet"; double myValue = 0.0; myFile >> myData >> myValue; assert(myData=="bilderbikkelWasHere"); return myValue; } int main() { writeToFile("temp.tmp",69.69); const double myValue = readFromFile("temp.tmp"); assert(myValue==69.69); }
- include <fstream>
- include <string>
- include <cassert>
Example #2
//A helper function bool fileExists(const std::string& fileName) { std::fstream file; file.open(fileName.c_str(),std::ios::in); if( file.is_open() == true) { file.close(); return true; } file.close(); return false; } std::vector<std::string> ReadFile(const std::string& fileName) { assert(fileExists(fileName)==true); std::fstream file(fileName.c_str(),std::ios::in); assert( file.is_open() == true); std::vector<std::string> v; while (file.eof() == false) { std::string s; file >> s; v.push_back(s); } file.close(); return v; }
- include <cassert>
- include <vector>
- include <string>
- include <fstream>
You could also use the Boost's serialization library.
Questions
- What are the options on opening a file I want to read from?
- What are the options on opening a file I want to write to ?
- How can I check if a file exists?
- How do I copy a file?
- How do I get a file's path?
- How do I get a filename's extension?
- How do I remove a filename's extension?
Windows XP issues
Windows XP automatically defragments the harddisk. If you open a file for longer then 13 minutes without using it, the opened file might get moved. Therefore, do not open your files without using them [1].Testing this, with the code below, for 100 minutes, however, does not result in an error.
int main() { //Start the timer measuring total program execution time boost::timer mainTimer; //Create 100 files const int nFiles = 100; std::vector<boost::shared_ptr<std::ofstream> > files; for (int i=0; i!=nFiles; ++i) { //Create a file const std::string fileName = "test" + boost::lexical_cast<std::string>(i) + ".txt"; files.push_back( boost::shared_ptr<std::ofstream>( new std::ofstream(fileName.c_str()) ) ); } //Write to and remove one file per minute for (int i=0; i!=nFiles; ++i) { //Create a timer boost::timer myTimer; //Wait one minute while (myTimer.elapsed() < 60.0) { /* wait */ } //Write to file (*files.back()) << "Test" << i << std::endl; //Close the file files.pop_back(); //Output to screen std::cout << "Did " << i << "th file" << std::endl; } //Finished. But will XP make use get at this point in the program? std::cout << "Finished without problems after " << mainTimer.elapsed() << " seconds" << std::endl; }
- include <fstream>
- include <string>
- include <iostream>
- include <vector>
- include <cassert>
- include <boost/timer.hpp>
- include <boost/lexical_cast.hpp>
- include <boost/shared_ptr.hpp>
'File I/O' links
Code links
- <<, stream out operator
- assert
- cassert (header file)
- const
- double
- ifstream
- iostream
- main
- ofstream
- return
- std
- std::ifstream
- std::ofstream
- stream out operator, <<
- string
References
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
