[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppOperator » Brainfuck » TurboCpp2006 » QbasicFAQ_CallAbs » CP FAQ » talk:Cpp » CppCompiler » server » VBAPIPipes » DOS » CppFileIo

(C++) File input/output

The reading from and writing to a file. Aften abbreviated to File I/O.

Example #1

  1. include <fstream>
  2. include <string>
  3. include <cassert>
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); }


Example #2

  1. include <cassert>
  2. include <vector>
  3. include <string>
  4. include <fstream>
//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; }


You could also use the Boost's serialization library.

Questions

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.
  1. include <fstream>
  2. include <string>
  3. include <iostream>
  4. include <vector>
  5. include <cassert>
  6. include <boost/timer.hpp>
  7. include <boost/lexical_cast.hpp>
  8. include <boost/shared_ptr.hpp>
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; }


'File I/O' links

Code links

References



last edited (May 29, 2007) by bilderbikkel, Number of views: 28750, Current Rev: 22 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Site Management by Lars Hagelin at Kontantkort.se.