Is there a way in C++ to grab a random number up to a given size then read that line from a text file? Without having to step through all the lines? I’ve got this which just prints out line by line:
#include <cstdio>
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc, char* argv[]){
ifstream myReadFile;
myReadFile.open("words.txt");
char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
printf("\n");
myReadFile >> output;
cout<<output;
}
}
myReadFile.close();
return 0;
}
There’s no way to do it unless you know the size of each line through some other means. Then you could add up the sizes of the lines you want to skip and do a
seekgto skip to the start of the line.