Chinaunix首页 | 论坛 | 博客
  • 博客访问: 350364
  • 博文数量: 89
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 951
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-23 11:51
个人简介

好懒,什么都没写

文章分类

全部博文(89)

文章存档

2012年(3)

2011年(17)

2007年(20)

2006年(49)

我的朋友

分类: C/C++

2011-06-02 12:53:28

 
写入文件,然后读取文件
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>

  4. void Approach1();
  5. void Approach2();

  6. void ProcessLine( std::string const & lineData );

  7. int main()
  8. {
  9.     // Use first approach to write and read file
  10.     std::cout << "Output from first approach:\n";
  11.     Approach1();

  12.     // Use second approach to write and read file
  13.     std::cout << "\nOutput from second approach:\n";
  14.     Approach2();

  15.     return 0;
  16. }

  17. // Write and read file using first approach.
  18. // Create a std::ofstream, write data to it and close stream.
  19. // Open file using a std::ifstream, read and process lines
  20. // from it.
  21. void Approach1()
  22. {
  23.         // Open file for writing, truncating file data
  24.         // if file exists.
  25.         std::ofstream out("approach1.dat");

  26.         // Write 4 lines to it
  27.         out << "line1\nline2\nline3\nline4\n";

  28.         // Explicitly close out file stream
  29.         // Would also be closed on destruction of object out
  30.         out.close();

  31.         // Open file for reading
  32.         std::ifstream in("approach1.dat");

  33.         // Read and process data in file
  34.         std::string lineData;
  35.         while (in)
  36.         {
  37.                 in >> lineData;

  38.                 if ( !in )
  39.                 {
  40.                         if ( !in.eof() )
  41.                         {
  42.                                 std::cerr << "Error! Input "
  43.                                              "stream failure. "
  44.                                              "Quitting.\n";
  45.                         }
  46.                 }
  47.                 else
  48.                 {
  49.                         ProcessLine( lineData );
  50.                 }
  51.         }

  52.         in.close();
  53. }

  54. // Write and read file using second approach
  55. // Create a std:fstream, open for read write.
  56. // Write data, rewind read position and read
  57. // and process lines in file.
  58. void Approach2()
  59. {
  60.         // Open file for reading and writing,
  61.         // with truncation of existing file data
  62.         std::fstream inOut( "approach2.dat"
  63.                           , std::ios::in | std::ios::out
  64.                                          | std::ios::trunc
  65.                           );

  66.         // Write 4 lines to it
  67.         inOut << "line1\nline2\nline3\nline4\n";

  68.         // Flush buffered written data to file:
  69.         // This is not strictly necessary but shows you
  70.         // how to do it (could also have used std::endl
  71.         // instead of last \n above)
  72.         inOut.flush();

  73.         // Rewind read position to start of file
  74.         inOut.seekg(0);

  75.         // Read and process data in file
  76.         std::string lineData;
  77.         while ( !inOut.eof() )
  78.         {
  79.                 inOut >> lineData;

  80.                 if ( !inOut )
  81.                 {
  82.                         if ( !inOut.eof() )
  83.                         {
  84.                                 std::cerr << "Error! Input "
  85.                                              "stream failure. "
  86.                                              "Quitting.\n";
  87.                         }
  88.                 }
  89.                 else
  90.                 {
  91.                         ProcessLine( lineData );
  92.                 }
  93.         }

  94.         inOut.close();
  95. }

  96. // Process a line of data.
  97. // If the line starts with "line" then asume it
  98. // has the correct format and is followed by a
  99. // single digit. Output this digit to std::cout.
  100. // otherwise output error to std:cerr.
  101. void ProcessLine( std::string const & lineData )
  102. {
  103.         std::string prefix("line");
  104.         if ( 0==lineData.find(prefix) )
  105.         {
  106.                 std::cout << "Line value:"
  107.                           << lineData.substr(prefix.length(),1)
  108.                           << '\n';
  109.         }
  110.         else
  111.         {
  112.                 std::cerr << "Error! badly formatted line.\n";
  113.         }
  114. }
 
参照:
 
阅读(4438) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~