Good day,
I'd like to read a file, 15 characters at a time, until the end of the file. Can someone tell me what functions should I use? Thanks.
Good day,
I'd like to read a file, 15 characters at a time, until the end of the file. Can someone tell me what functions should I use? Thanks.
Just use fgets
Ofcourse, do all the error checking for pointer fp before using it.Code:FILE *fp; char buff[15]; fp=fopen("abc.txt","r"); while ((fgets(buff,sizeof(buff),fp)) != NULL) { /*process the chunk here */ }
So on the next call of fgets() it will continue from where it stopped?
Yes, the file pointer keeps track of that stuff.Originally Posted by caduardo21
Look up the function fseek, maybe it will make more sense to you then.
{RTFM, KISS}
Whoa, you don't want to use fgets() to read 15 bytes at a time. Look up the the function fread() instead. fgets() will stop reading whenever it hits a newline. Not to mention that passing 15 as the 2nd parameter to fgets() will actually only read 14 bytes since it leaves room for a \0.
If you understand what you're doing, you're not learning anything.