C ++从文件末尾向后读取文件 [英] C++ Reading file backwards from the end of the file

查看:186
本文介绍了C ++从文件末尾向后读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个菜单写一个程序,从一个文本文件中读取几种不同的方式。我只是工作菜单选项#2仍然(从文件结尾处向后读取),但我不能围绕我做错了。我已经在这里几天了,只是找不到任何好的资源,帮助这一点。任何帮助将不胜感激。

I am trying to write a program with a menu that reads from a text file a few different ways. I'm just working on menu option #2 still (reading backwards from the end of the file), but I can't wrap my head around what I'm doing wrong. I've been at this for a few days now and just can't find any good resources to help on this. Any help would be appreciated.

#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <math.h>
#include <fstream>

using namespace std;

const int SIZE = 20;                     
typedef char String[SIZE];

//prototypes
void Menu(int &);
void ReadFile(ifstream &);
void BackwardFile(ifstream &,long &);
long CountFile(ifstream &,long &);

int main()
{  
    char filename[]= "grades.txt";
    int choice;
    long numBytes;

    ifstream InList;
    InList.open(filename);

    if(InList.good())
    {
        do
        {         
            Menu(choice); 
            switch(choice)
            {
                case 1:  
                    ReadFile(InList);
                    break;
                case 2:
                    CountFile(InList,numBytes);
                    BackwardFile(InList,numBytes);
                    break;
                case 3:
                    cout << "Pick a start position between 0 - " << numBytes << endl;
                    break;
                /*case 4:*/

                case 5:
                    cout << "\n GOOD BYE." << endl << endl; 
                    break;
            }

        }while (choice != 5);                         
    }
    else
        cout << "File did not open successfully." << endl << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

void Menu(int &choice) 
{ 
    cout << "\n    Choose an option:";
    cout << "\n...................................";
    cout << "\n 1- Display All Contents of the File";
    cout << "\n 2- Display Content in Reverse Order";
    cout << "\n 3- Display from Point A to Point B";
    cout << "\n 4- Display from Point B to Point A";
    cout << "\n 5- Exit";
    cout << "\n\n Enter your choice: ";
    cin >> choice; 
} 

void ReadFile(ifstream& inFile)
{
    char byte;

    inFile.clear();

    cout<< "\nReading contents from the file:" <<endl;
    if(inFile)
    {
        inFile.get(byte);
        while(inFile.good())
        {
            cout << byte;
            inFile.get(byte);  
        }
    }
    inFile.close();
}

void BackwardFile(ifstream& inFile, long& numBytes)
{
    char byte;

    inFile.clear();
    cout<< "\nReading contents backwards from the file:" <<endl;

    inFile.seekg(numBytes, ios::end);

    while(inFile)
    {
        inFile.get(byte);               
        cout << byte;
        numBytes--;
        inFile.seekg(numBytes);  
    }
    inFile.close();
}                              

long CountFile(ifstream& inFile, long& numBytes)                           
{
    inFile.seekg(0L, ios::end);
    numBytes = inFile.tellg();
    return numBytes;
}


推荐答案

问题。
当打开文件时,我使用ios :: ate将文件位置设置为文件的结尾
,并使用seekg方法读回。
我不知道是否有更有效的方法来解决这个问题。

The following is my solution to the question. When open the file, I use ios::ate to set the file position to the end of the file and use seekg method to read back. I am not sure whether there is a more efficient way to solve this question.

void readFile(char *fileName){
    char c;
    std::ifstream myFile(fileName,std::ios::ate);
    std::streampos size = myFile.tellg();
    for(int i=1;i<=size;i++){
        myFile.seekg(-i,std::ios::end);
        myFile.get(c);
        printf("%c\n",c);
    }
}

这篇关于C ++从文件末尾向后读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆