简体   繁体   中英

How to read a file, reverse part of the text and write that reversed part on another file on C++?

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

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.

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM