I tried looking for similar questions that could find an answer to my question but i couldn't seem to find it. Could someone please tell me is there any way to read certain lines from the .txt file. For example i would like to read lines 2,3,5 from the text file.
Example of text file:
1255466889436 //Line 1 - I want to read this line
5489784642165 //Line 2
4984651425165 //Line 3 - I want to read this line
4968164816514 //Line 4
4161654168468 //Line 5 - I want to read this line
It would be nice if somone could show example code cause im really stumped at the moment.
Read all lines, discard the ones you don't need.
You can seek to any offset in a file, but which line is offset 5000? That depends on how many newline characters there were in the previous 5000 characters. And unless you check all characters, you don't know.
You can read all the lines and just ignore those that you do not need. Remember that you start from the beginning of the file and you keep reading the lines until you find those that you want or until the end of the file.
try this : START READING FILE FROM FIRST CHARACTER TILL THE END OF FILE BY CHECKING value of eof(),
Till End of file is reached(feof(fp)==0),
with the curent byte position as 0th byte or first byte of first line, we are reading the first line by fgets() till newline characte is reached. Once \n is reached, fgets() tops its function and the ead content is stored in data array. the string length in this aray is the total number of characters/bytes in first line. So adding 1 byte more than that, now the current byte position becomes the beginning byte of the next line.