1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
//This program reads a file from beg to end, end to beg, beg to 4th position,
//8th to 15th position, end to 21st position, and 22nd position to the end
#include <iostream>
#include <fstream>
using namespace std;
void begToEnd(fstream &, char);
void endToBeg(fstream &, char);
void begTo4th(fstream &, char);
void eighthTo15th(fstream &, char);
void endTo21st(fstream &, char);
void Twenty2ndToEnd(fstream &, char);
int main()
{
char letters; //holds the character
int choice;
fstream file("alphabet.txt", ios::in | ios::binary); //This opens the file
do
{
cout << "Enter 1 to read from beginning to end" << endl
<< "Enter 2 to read from end to beginning" << endl
<< "Enter 3 to read from beginning to 4th position" << endl
<< "Enter 4 to read from 8th to 15th position" << endl
<< "Enter 5 to read from end to 21st position" << endl
<< "Enter 6 to read from 22nd position to the end" << endl
<< "Choice: ";
cin >> choice;
switch(choice)
{
case 1:
begToEnd(file, letters);
break;
case 2:
endToBeg(file, letters);
break;
case 3:
begTo4th(file, letters);
break;
case 4:
eighthTo15th(file, letters);
break;
case 5:
endTo21st(file, letters);
break;
case 6:
Twenty2ndToEnd(file, letters);
break;
}
cout << endl;
system("pause");
file.close(); //Closes the file
}
while(choice != 'N' && choice != 'n');
cout << "Would you like to continue? Y/N: ";
cin >> choice;
return 0;
}
void begToEnd(fstream &in, char letter)
{
in.seekg(0L, ios::beg); //Displays beginning to end
for(int i = 0; i < 25; i++)
{
in.get(letter);
cout <<"Beginning to end: " << letter << endl;
}
}
void endToBeg(fstream &in, char letter)
{
in.seekg(0, ios::end); //Displays end to beginning
int size = in.tellg();
for (int i=1; i <= size; i++)
{
in.seekg(-i, ios::end);
letter=in.get();
cout << "End to beginning: " << letter << endl;
}
}
void begTo4th(fstream &in, char letter)
{
in.seekg(0L, ios::beg); //Displays beginning to 4th position
for(int i = 0; i < 4; i++)
{
in.get(letter);
cout << "Beginning to the 4th letter: " << letter << endl;
}
}
void eighthTo15th(fstream &in, char letter)
{
in.seekg(7L, ios::beg); //Displays 8th to 15th position
for(int i = 0; i < 7; i++)
{
in.get(letter);
cout << "8th to 15th letter: " << letter << endl;
}
}
void endTo21st(fstream &in, char letter)
{
in.seekg(0, ios::end); //Displays end to 21st position
int size = in.tellg();
for (int i=1; i <= 5; i++)
{
in.seekg(-i, ios::end);
letter=in.get();
cout << "End to 21st: " << letter << endl;
}
}
void Twenty2ndToEnd(fstream &in, char letter)
{
in.seekg(21L, ios::beg); //Displays the 22nd position to the end
for(int i = 0; i < 5; i++)
{
in.get(letter);
cout << "22nd to end: " << letter << endl;
}
}
|