How reverse text in text file using C++

1 Answer

0 votes
#include <iostream>
#include <string>
#include <fstream>

using std::cout;
using std::endl;

int main()
{
	std::ifstream ifs("d:\\data.txt");

	std::string s((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));

	ifs.close();
	
	if (!isalpha(s[s.size() - 1]))
		s.pop_back();

	reverse(s.begin(), s.end());

	std::ofstream ofs("d:\\data.txt");
	ofs << s;
	ofs.close();

	return 0;
}

/*
run:

nohtyp php avaj #c
++c c

*/

 



answered Jun 11, 2018 by avibootz

Related questions

1 answer 217 views
1 answer 188 views
188 views asked Jun 10, 2018 by avibootz
1 answer 215 views
1 answer 148 views
1 answer 119 views
119 views asked Jan 29, 2023 by avibootz
1 answer 110 views
110 views asked Jan 29, 2023 by avibootz
...