Need a project done?

C++ Programming Developer

Search This Blog

Printing random lines from file C++

Printing random lines from file in C++ program is given below:

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;

int main(){
ofstream os("a.txt");
for (int i = 0; i<=999; i++){
os << i << endl;
}
os.close();

ifstream is("a.txt");

char c;
string line;
while (!is.eof()){
while (c != '\n'){
is.seekg(rand()%1000);
c = is.get();
}
getline(is,line);
cout << line << endl; //Printing lines randomly from file.
c = '0';
Sleep(300);
}
return 0;
}


Though lines are being printed randomly but there is repetition.

No comments:

Post a Comment

"Don't let anyone ever make you feel like you don't deserve what you want."