[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

understanding fgets

arnuld

7/26/2011 7:53:00 AM

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");
}

}



--
www.lispmachine.wordpress.com
find my email-ID @above blog
13 Answers

Nick Keighley

7/26/2011 8:05:00 AM

0

On Jul 26, 8:53 am, arnuld <arnuld.miz...@gmail.com> wrote:

> 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>

arnuld

7/26/2011 9:26:00 AM

0

> On Jul 26, 1:05 pm, Nick Keighley <nick_keighley_nos...@hotmail.com> wrote:

> 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) :)

Ben Bacarisse

7/26/2011 11:23:00 AM

0

Nick Keighley <nick_keighley_nospam@hotmail.com> writes:

> On Jul 26, 8:53 am, arnuld <arnuld.miz...@gmail.com> wrote:
>
>> 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"

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>
--
Ben.

Chad

7/26/2011 4:13:00 PM

0

On Jul 26, 2:25 am, arnuld <arnuld.miz...@gmail.com> wrote:
> > On Jul 26, 1:05 pm, Nick Keighley <nick_keighley_nos...@hotmail.com> wrote:
> > 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) :)

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.

Chad

7/26/2011 4:15:00 PM

0

On Jul 26, 9:12 am, Chad <cdal...@gmail.com> wrote:
> On Jul 26, 2:25 am, arnuld <arnuld.miz...@gmail.com> wrote:
>
> > > On Jul 26, 1:05 pm, Nick Keighley <nick_keighley_nos...@hotmail.com> wrote:
> > > 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) :)
>
> 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*

Willem

7/26/2011 4:22:00 PM

0

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

Chad

7/26/2011 4:35:00 PM

0

On Jul 26, 9:21 am, Willem <wil...@toad.stack.nl> wrote:
> 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.

Kenneth Brody

7/26/2011 5:19:00 PM

0

On 7/26/2011 12:15 PM, Chad wrote:
> On Jul 26, 9:12 am, Chad<cdal...@gmail.com> wrote:
>> On Jul 26, 2:25 am, arnuld<arnuld.miz...@gmail.com> wrote:
>>
>>>> On Jul 26, 1:05 pm, Nick Keighley<nick_keighley_nos...@hotmail.com> wrote:
>>>> 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) :)
>>
>> 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*

er *do this in one line of Haskell*. :-)

--
Kenneth Brody

Seebs

7/26/2011 6:42:00 PM

0

On 2011-07-26, arnuld <arnuld.mizong@gmail.com> wrote:
> 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
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Ben Bacarisse

7/26/2011 7:51:00 PM

0

Seebs <usenet-nospam@seebs.net> writes:

> On 2011-07-26, arnuld <arnuld.mizong@gmail.com> wrote:
>> 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".

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

--
Ben.