understanding fgets()

A

arnuld

I wrote this program which reads a file and prints it onto stdout
using fgets().

PROBLEM: fgets() reads line by line into an array. Lets say, in input
file, first line is longer than 2nd line. Then after reading first
line into array all characters read must be present in the array when
it reads 2nd line e.g.

1st line: usenet
2nd line: clc

after reading first line contents of array: usenet
(array is not memset()ed)
after reading second line contents of array: clc (why not clcnet)

which means /use/ of /usenet/ was not replaced by /clc/ but contents
of array were replaced/cleared. (slashes are used for readability
here, emphasizing single words).

QUESTION: My question is I did not clear array contents after one call
to fgets() then who did ? fgets() ?



#include <stdio.h>
#include <stdlib.h>

enum { FGETS_READ_MAX = 256 };

void print_file(FILE* p);

int main(void)
{
const char* filename = "dp.conf";
FILE* filep;

filep = fopen(filename, "r");
if(NULL == filep)
{
fprintf(stderr,"IN: %s @%d: ERROR opening file\n", __FILE__,
__LINE__);
return EXIT_FAILURE;
}

print_file(filep);

if(fclose(filep))
{
printf("IN: %s @%d: ERROR closing file\n", __FILE__, __LINE__);
}

return 0;
}



void print_file(FILE* p)
{
char arrc[FGETS_READ_MAX + 2] = {0};

while(fgets(arrc, FGETS_READ_MAX, p))
{
printf("\t::%s", arrc);
}

if(feof(p))
{
printf("Successful EoF EXIT :)\n");
}
else if(ferror(p))
{
printf("IN: %s @%d: ERROR reading file\n", __FILE__, __LINE__);
}
else
{
printf("OOPS! .. some other kind of issue ??\n");
}

}
 
N

Nick Keighley

I wrote this program which reads a file and prints it onto stdout
using fgets().

PROBLEM: fgets() reads line by line into an array. Lets say, in input
file, first line is longer than 2nd line. Then after reading first
line into array all characters read must be present in the array when
it reads 2nd line e.g.

1st line:  usenet
2nd line:  clc

after reading first line contents of array: usenet
(array is not memset()ed)
after reading second line contents of array: clc (why not clcnet)

fgets terminates it's input with a nul char. So the array actually
holds

"clc\0et\0"

printf(0ing the buffer stops at the first nul char

<snip>
 
A

arnuld

fgets terminates it's input with a nul char. So the array actually
holds

  "clc\0et\0"
..SNIP..

Thanks Nick. I think I am taking a U-turn from a C hater -> C (not
that good) -> C (brilliant) :)
 
B

Ben Bacarisse

Nick Keighley said:
fgets terminates it's input with a nul char. So the array actually
holds

"clc\0et\0"

A detail: fgets retains the newline (when there is room for it) so the
first call most likely produced

"usenet\n\0"

and the second one resulted in

"clc\n\0t\n\0"

There has been some debate about whether fgets has permission to alter
the array elements beyond the null that it is obliged to write.
Whatever view you take on this point, it is unlikely that an
implementation will actually do this and therefore the presence of
"t\n\0" is a pretty sound bet.

<snip>
 
C

Chad

Thanks Nick. I think I am taking a U-turn from a C hater -> C (not
that good) -> C (brilliant) :)

I guess it depends on what your doing. For example, say I want to
write a program that calculates the factorial of 1000!. I can do this
in line of Haskell and get the exact number. However, doing the exact
same thing in C would require a lot more work.
 
C

Chad

I guess it depends on what your doing. For example, say I want to
write a program that calculates the factorial of 1000!. I can do this
in line of Haskell and get the exact number. However, doing the exact
same thing in C would require a lot more work.

er *do this one line of Haskell*
 
W

Willem

Chad wrote:
) I guess it depends on what your doing. For example, say I want to
) write a program that calculates the factorial of 1000!. I can do this
) in line of Haskell and get the exact number. However, doing the exact
) same thing in C would require a lot more work.

Indeed.
You would have to locate and download a library for manipulating large
numbers, and then write so many lines of code, perhaps even ten or more.

Any more silly examples ?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
C

Chad

Chad wrote:

) I guess it depends on what your doing. For example, say I want to
) write a program that calculates the factorial of 1000!. I can do this
) in line of Haskell and get the exact number. However, doing the exact
) same thing in C would require a lot more work.

Indeed.
You would have to locate and download a library for manipulating large
numbers, and then write so many lines of code, perhaps even ten or more.

Any more silly examples ?

Nope.
 
S

Seebs

PROBLEM: fgets() reads line by line into an array. Lets say, in input
file, first line is longer than 2nd line. Then after reading first
line into array all characters read must be present in the array when
it reads 2nd line e.g.
Huh?

1st line: usenet
2nd line: clc
after reading first line contents of array: usenet
(array is not memset()ed)
after reading second line contents of array: clc (why not clcnet)

No.

After reading first line, contents of array: "usenet\0".

After reading second line, contents of array: "clc\0et\0".

-s
 
B

Ben Bacarisse

Seebs said:
No.

After reading first line, contents of array: "usenet\0".

After reading second line, contents of array: "clc\0et\0".

You mean "usenet\n\0" then "clc\n\0t\n\0".
 
M

Malcolm McLean

Chad wrote:

) I guess it depends on what your doing. For example, say I want to
) write a program that calculates the factorial of 1000!. I can do this
) in line of Haskell and get the exact number. However, doing the exact
) same thing in C would require a lot more work.

Indeed.
You would have to locate and download a library for manipulating large
numbers, and then write so many lines of code, perhaps even ten or more.

Any more silly examples ?
No, it's a fair point.

High precision maths libraries don't ship as standard in C. So you
have to locate a library, then install it, and that's often difficult.
Most people actually find it easier to incorporate most libraries as
source. There is often quite a bit of fiddling to do. Then it might
have declared bool and necessitate a rewriting of your own source.

It's also virtually impossible to read mathematical expressions when
the arithmetical operators are replaced with function calls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top