Q: given a txt file, create a new one out of it by reversing the symbols from it and flipping the strings upside down.
Ex:
(input file)
q w e r
a s d f
z x c v
==
(output)
v c x z
f d s a
r e w q
here's what i got but it doesn't flip the lines but simply outputs it in reverse as one line. and it doesn't put it in a new file
I was thinking of starting with something like this:
1 2 3 4 5
std::ifstream file("numbers.txt");
// <--- Should check if file is open and usable.
std::string content, flippedContent;
std::vector<std::string> numbers;
std::vector<std::string> flippedNumbers;
After reading the file into the first vector flip the string and store it in the new vector then you can output the new vector in reverse order.
I have not change any code yet. It will take a few minutes.
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
usingnamespace std;
int main()
{
std::istringstream file
{
"q w e r\n""a s d f\n""z x c v\n"
};
//std::ifstream file("numbers.txt");
// <--- Should check if file is open and usable.
std::string content, flippedContent;
std::vector<std::string> lines;
std::vector<std::string> flippedNumbers;
while (std::getline(file, content))
lines.push_back(content);
for (size_t idx = 0; idx < lines.size(); idx++)
{
content = lines[idx];
for (auto rit = content.rbegin(); rit < content.rend(); rit++)
{
flippedContent += *rit;
}
flippedNumbers.emplace_back(flippedContent);
flippedContent.clear();
}
for (auto rit = flippedNumbers.rbegin(); rit < flippedNumbers.rend(); rit++)
std::cout << *rit << '\n';
std::cout << std::endl;
return 0; // <--- Not required, but makes a good break point.
}
> reversing the symbols from it and flipping the strings upside down
We can't 'flip the strings upside down' in one pass other than by reading in the entire contents first. Reading the contents into a string and writing that string in reverse would do the job. For example:
#include <iostream>
#include <string>
#include <fstream>
// read the entire contents into a string
std::string get_text( std::ifstream file )
{
std::string text ;
char c ;
while( file.get(c) ) text += c ;
return text ;
}
// write reverse "upside down"
bool write_flipped( std::ofstream file, const std::string& text )
{
// avoid creating a second potentially very long temporary string
for( auto iter = text.rbegin() ; iter != text.rend() ; ++iter ) file.put( *iter ) ;
returnbool(file) ;
}
int main()
{
const std::string input_file_name = "in.txt" ;
const std::string output_file_name = "out.txt" ;
// create a test file
std::ofstream(input_file_name) << "1. first line - A\n2. the second line - B\n3. and a third line - C\n" ;
// read the input file into a string and write it in reverse
const std::string text = get_text( std::ifstream(input_file_name) ) ;
write_flipped( std::ofstream(output_file_name), text ) ;
// dump the contents of the two files to verify the result
std::cout << "input file " << input_file_name << "\n-----------\n"
<< std::ifstream(input_file_name).rdbuf() ;
std::cout << "\noutput file " << output_file_name << "\n-----------\n"
<< std::ifstream(output_file_name).rdbuf() ;
}