How to get random character from text file in 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);

	char ch;
	out.get(ch);

	cout << ch << endl;

	out.close();

	return EXIT_SUCCESS;
}

/*
run:

g

*/

 



answered Jun 16, 2018 by avibootz

Related questions

...