How to reverse the first N characters within a text file in C++

1 Answer

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

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

int main()
{
	static const char *fileName = "d:\\data.txt";
	std::string s;

	{
		std::ifstream file(fileName); 
		char ch;
		while (file.get(ch))
			s += ch; 
	}

	int N = 3;
	std::reverse(s.begin(), s.begin() + N);

	{
		std::ofstream file(fileName); 
		file << s;
	}

	return 0;
}

/*
run:

++c c
c# java php python

*/

 



answered Jun 16, 2018 by avibootz

Related questions

1 answer 176 views
1 answer 163 views
1 answer 165 views
1 answer 155 views
155 views asked Jun 11, 2018 by avibootz
1 answer 201 views
...