difference between fgets and fgetl

4 Ansichten (letzte 30 Tage)
Alp
Alp am 31 Dez. 2012
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!!!!

Antworten (1)

Walter Roberson
Walter Roberson am 31 Dez. 2012
Bearbeitet: Walter Roberson am 31 Dez. 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.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by