filereverse.cpp

来自「适合初学者学习以及程序员回顾」· C++ 代码 · 共 50 行

CPP
50
字号
// FileReverse.cpp

#include <iomanip>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::setw;

#include <fstream>
using std::fstream;
using std::ios;

char* FileNameIn  = "Original.txt";
char* FileNameOut = "Reversed.txt";

// ---主程式------------------------
int main()
 {
   long CharNum, Offset;
   char Ch;
   fstream FileInput(FileNameIn, ios::in);
   if (!FileInput)
     {cout << "档案 "  << FileNameIn
      << " 开启失败!"  << endl; exit(1);}
   fstream FileOutput(FileNameOut, ios::out);
   if (!FileOutput)
     {cout << "档案 "  << FileNameOut
      << " 存档失败!"  << endl; exit(1);}
   cout << "档案  "    << FileNameIn
        << " 开启成功" << endl;

   FileInput.seekg(0L, ios::end);  //移到档案结束的地方
   CharNum = FileInput.tellg();    //获得字元数目

   for (Offset=1L; Offset<= CharNum; Offset++)
     {
       FileInput.seekg(-Offset, ios::end);
       Ch = FileInput.get();
       FileOutput << Ch;
     }
   
   FileOutput.close();
   FileInput.close();
   cout    << "倒转档案 " << FileNameIn
           << " 后的内容\n成功存于档案 " 
           << FileNameOut
           << " 内." << endl;
 }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?