+ 2

Question about fgets() in c language

It says that fgets reads a line from string so what if i want to read multiple lines?

22nd May 2022, 2:55 AM
asing red
4 Respuestas
+ 2
Use fgets() multiple times, it automatically advances the file pointer to read the next line. Every function that reads from a file advances the file pointer, so you'll never read the same thing twice. You can even mix the functions (fgets(), fgetc(), fscanf(), fread() etc), and each will continue from where the last one left off. IF you know you'll want to read the same thing twice, you'll have to manually reposition yourself in the file (either save the position before each read that you think you'll need to repeat, or, if the file is opened binary, you can use relative positioning and turn back by the length of the last read; but it's probably better to save the position, as text mode makes translation of newline and sometimes the length of the last read isn't the same as how much the file pointer moved).
23rd May 2022, 1:01 AM
lion
lion - avatar
+ 1
You probably will need to have a 2D char array into which you will store the lines. Can you describe the idea of reading multiple lines more clearly? for what purpose?
22nd May 2022, 4:37 AM
Ipang
+ 1
Ipang for example i have a .txt file that has 3 lignes each line has a number of letters and i want to to read the full thing since fgets only stops reading after a new ligne how do i make it so it reads the full thing ( sorry maybe my example is bad I still find files hard to understand)
22nd May 2022, 5:45 AM
asing red
+ 1
lion tysm 🙏
23rd May 2022, 1:12 AM
asing red