Как использовать файлы произвольного доступа для чтения из одной точки в другую - PullRequest
0 голосов
/ 13 апреля 2020

Я работаю над программой, которая использует файлы с произвольным доступом и читает текстовый файл определенным образом в зависимости от того, что запрашивает пользователь. Вариант 4 должен запрашивать у пользователя начальную и конечную точки в этом текстовом файле и читать эту часть файла. Я вроде понимаю, как заставить код запрашивать конечную точку, но я не уверен, как заставить код запрашивать начальную точку. Это мой код и то, что у меня есть.

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <iomanip>

using namespace std;

int main() {
  char filename[]="file.txt"; 
  char content[10];
  ifstream inFile;
  int choice;
  int option;
  int amount_read = 0;
  int amount_to_be_read;
  l1: cout << "Choose an option to decide what you want to do with the file." << endl;
  cout << "Option 1: Beginning to End."<< endl;
  cout << "Option 2: End to Beginning."<< endl;
  cout << "Option 3: Beginning to Certain Point."<< endl;
  cout << "Option 4: Certain Point to Certain Point. "<< endl;
  cin >> choice;

  if (choice==1){

  inFile.open(filename);
  if(inFile.fail())
    {
         cout << "file named can not be found \n";
         system("pause");
         exit(1);
    }
    inFile>>content;
    while(inFile.good()) 
    {
     cout <<content<< ' ' <<endl;
     inFile>>content;
     }
        inFile.close();

    cout << "Do you want to go again? 1 for Yes and 2 for No."<< endl;
    cin >> option;
    if (option== 1)
    {
      goto l1;
    }
    else
    {
      terminate();
    }
  }

  if (choice==2){

    inFile.open(filename);
    if(inFile.fail())
    {
         cout << "file named can not be found \n";
         system("pause");
         exit(1);
    }

    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",c);
    }
    cout << "Do you want to go again? 1 for Yes and 2 for No."<< endl;
    cin >> option;
    if (option== 1)
    {
      goto l1;
    }
    else
    {
      terminate();
    }
  }

  if (choice==3){

    inFile.open(filename);
    if(inFile.fail())
        {
         cout << "file named can not be found \n";
         system("pause");
         exit(1);
        }

    cout << "Where do you want to read up to?";
    cin >> amount_to_be_read;

    inFile>>content;
    while(inFile.good() && amount_read < amount_to_be_read) 
    {
        cout <<content<< ' ' <<endl;
        inFile>>content;
        amount_read++;
    }
        inFile.close();
    cout << "Do you want to go again? 1 for Yes and 2 for No."<< endl;
    cin >> option;
    if (option== 1)
    {
      goto l1;
    }
    else
    {
      terminate();
    }



  }

  if (choice==4){
    inFile.open(filename);
    if(inFile.fail())
        {
         cout << "file named can not be found \n";
         system("pause");
         exit(1);
        }
  }
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...