Answer to Question #273908 in C++ for Perkit

Question #273908



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.

1
Expert's answer
2021-12-02T00:35:32-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS