Read in the contents of a file of real numbers for which the file length is not known ahead of time and could be large. Write the numbers to a new file in reverse order.
Note: Store the data in a vector before writing the numbers to the output file in reverse order.
#include <conio.h>
#include <fstream>
#include <iostream>
#include <stdio.h>
void reverse(char S[])
{
char option;
std::ofstream O;
O.open("Real.txt", ios::out);
for (int i = 0; S[i] != '\0'; i++) {
O.put(S[i]);
}
int position = O.tellp();
O.close();
std::ifstream ifs;
ifs.open("Real.txt", ios::in);
ofstream ofs1;
ofs1.open("Real2.txt", ios::out);
ifs.seekg(--position);
while (position >= 0) {
ifs.get(option);
ofs1.put(option);
position--;
ifs.seekg(position);
}
ifs.close();
ofs1.close();
ifstream ifs1;
ifs1.open("Real2.txt", ios::in);
while (!ifs1.eof()) {
ifs1.get(option);
cout << option;
}
ifs1.close();
}
int main()
{
clrscr();
cout << "example 1: (Real For Real) \n";
reverse("Real For Real");
cout << "\n example 2:(reverse)\n";
reverse("reverse");
getch();
return 0;
}
Comments
Leave a comment