0

I need help, I wrote the code, did the reverse thing but I can't get it written on another file.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream par2("C:/fajllat/f1.bin", ios::in);
    string line;

    for (int i = 1; !par2.eof() ; i++)
    {
        getline(par2, line);

        if (i < 5 || i >14)  continue;
        line = string(line.rbegin(), line.rend());

    }
    par2.close();

    ofstream mbrapsht ("C:/fajllat/f3.bin", ios::out);

    mbrapsht << line;

    mbrapsht.close();
    cin.get();cin.get();

    return 0;
}

When I check the files the f3.bin file is empty

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • 1
    A) please indent the code, b) [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Borgleader May 03 '18 at 01:30
  • 1
    C) [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Borgleader May 03 '18 at 01:34
  • I used " if (i < 5 || i >14) continue " so it will reverse only that part of the text, not the whole text. –  May 03 '18 at 01:35
  • Ah, that would clarify some of my confusion. Your `line = string(line.rbegin(), line.rend());` Will overwrite all of your previous work. – Captain Giraffe May 03 '18 at 01:39
  • If you want to learn how to write a string to a file, write a program that does *that and nothing else.* – Beta May 03 '18 at 01:46
  • @Beta I know how to write a string to a file, in fact I did the whole input file that I'm using in this code, I just can't get my head around writing it on reverse. –  May 03 '18 at 01:49

2 Answers2

1

You have the right idea. What you're missing is that if you want to write the reversed lines, you need to either write them inside the loop or store them for after. You are doing neither of these things.

Currently what happens is you overwrite line every loop. And whatever is left in that string is what you write afterwards. Turns out that for your case, that's an empty string.

Let's make minimal changes to what you have:

// (*) Open the output file before looping
ofstream mbrapsht("C:/fajllat/f3.bin", ios::out);

for (int i = 1; !par2.eof() ; i++)
{
   getline(par2, line);
   if (i < 5 || i > 14)  continue;
   line = string(line.rbegin(), line.rend());

   // (*) output the line - you also probably want an end-of-line
   mbrapsht << line << std::endl;
}

Now, it's okay-ish. But it does have a problem where if getline fails, your code still runs the loop body one more time. This happens if getline hits the end of file (or some other error state), which your loop doesn't pick up until the next iteration (or possibly never, if the error is not EOF).

So, a somewhat better choice might be:

for(int lineNo = 1; std::getline(par2, line); ++lineNo)
{
    if (lineNo >= 5 && lineNo <= 14)
    {
        std::reverse(line.begin(), line.end());  // (*) requires <algorithm>
        mbrapsht << line << std::endl;
    }
}

Note that I also inverted your test condition and removed the continue. In general, I avoid continue and break in loops unless not using them results in code that is hard to follow or understand at a glance. It's a style/maintainability thing. Take it or leave it.

paddy
  • 60,864
  • 6
  • 61
  • 103
0

See this snippet . For line-by-line reversal, you can use getline() instead and reverse before pushing into vector<string>.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;
int main()
{
    string str;
    ifstream par2("D:\\MyFolder\\try.txt", ios::in);

    if (par2.is_open())
    {
        stringstream strStream;
        strStream << par2.rdbuf();
        str = strStream.str();
        cout << str << endl;
        par2.close();
    }

    cout << "\n\nReversing ...\n\n";
    std::reverse(str.begin(), str.end());
    cout << str << endl;


    ofstream mbrapsht("D:\\MyFolder\\try2.txt", ios::out);
    mbrapsht << str;
    mbrapsht.close();

    return 0;
}

Output:

enter image description here

seccpur
  • 4,996
  • 2
  • 13
  • 21