How to insert character into a random place in a text file with C++

1 Answer

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

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

int main(void)
{
	static const char *fileName = "d:\\test.txt";

	std::fstream out(fileName, std::ios::in | std::ios::out);
	if (!out) {
		cout << "Error open file.";
		return EXIT_FAILURE;;
	}

	srand(time(NULL));
	int n = (rand() % 10) + 1;

	out.seekp(n, std::ios::beg);
	out.put('W');

	out.close();

	return EXIT_SUCCESS;
}

/*
run:

test.txt
--------
c++ programming -> c++ progWamming

*/

 



answered Jun 16, 2018 by avibootz

Related questions

1 answer 181 views
1 answer 179 views
1 answer 161 views
2 answers 138 views
1 answer 255 views
1 answer 175 views
...