difference between fgets and fgetl
7 views (last 30 days)
Show older comments
I was studying fgets and fgetl and i totally did not understand the difference between these two commands. Can u explain it and give a small example? Thanx in advance.
HAPPY NEW YEAR!!!!
0 Comments
Answers (1)
Walter Roberson
on 31 Dec 2012
Edited: Walter Roberson
on 31 Dec 2012
fgets() leaves the end-of-line characters in the string that is returned; fgetl() removes them from what is returned.
For example, if we let \r\n represent the end-of-line stored, then if the line in the file is
This is a test.\r\n
then fgets() would return
'This is a test.\r\n'
Not literal '\' 'r' '\' 'n' but the characte-return and end-of-line character,
['This is a test.' char(13) char(10)]
which is
sprintf('This is a test.\r\n')
On the other hand, fgetl() for the same line would return just
'This is a test.'
The only reason I have ever had to use fgets() was on systems old enough that they did not provide an fgetl() call. It isn't that I cannot think of any reason to use it, but in those few cases, it almost always turns out to be better to read as binary instead of as text.
0 Comments
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!