Article View: comp.std.c
Article #3293Re: C things
From: Norman Diamond
Date: Mon, 25 Jan 1993 01:33
Date: Mon, 25 Jan 1993 01:33
65 lines
3772 bytes
3772 bytes
In article <107937@bu.edu> spacefox@acs.bu.edu (Godfrey Degamo) writes:
Here we go again, comp.std-and-homework-server.c. These issues are no
different in the C standard than they were in the language 15 years ago.
Aren't there any tutors at BU that you can ask to help teach you?
> I would like to know why it's "dangerous" to use gets(). I always
>make certain I pass in the name of a char array or some pointer char that
>has been allocated some memory. It always seems to work for me.
It worked for a program called "fingerd" as well. "fingerd" passed a pointer
to a pointer char that had been allocated some memory. And read characters,
and read, and read, and read, and read, and kept reading. It read enough
characters to step on its own run-time stack and execute a virus that it
happily read in.
Of course, no one's likely to type a virus as input to your program. But
when they type more characters that you've allocated, your program likely
won't work as well for them as it works for you.
> What other alternative is there for one to use?
The documentation of fscanf() is very close to the documentation of scanf().
Read it and notice what the differences are. One difference is important here.
>I seem to have problems with scanf. For instance:
> scanf ("%s", &inputA);
> scanf ("%s", %inputB);
> What happens is that inputA gets the user input, then when the computer
>comes to the second line, it reads the CR from the first input as the input
>for inputB.
Exactly as documented.
>(I don't remember this: but, I think that I tried a variation
>"%s\r" and it still wouldn't work.)
The scanf() function (and most input functions, in fact all input functions
while operating in an ANSI/ISO standard C environment) never see \r. They
see \n. The operating system usually accepts \r from the keyboard in order
to be friendly to the user, but sends \n to the program. Anyway, if you want
to read just one character and throw it away, there is a function that will
read one character, and there is a format code to scanf() that will read one
character, and you don't even have to care which character it is.
> How do I modify then save the records of one person without loading up
>all the data then saving all the data
If you know where the record is, use fsetpos(). If you don't know where the
record is, you have to read until you find it. Maybe when you first create
the file, you should write some information to another file to help you
remember later where each record was, but the standard doesn't provide any
help for this.
>the concept of Random Access Files and Sequential Files
In standard C, you can always access a file sequentially, and you can always
try random access by calling fsetpos(), fgetpos(), etc. If there is some
reason why random access functions won't work (e.g. if the file is a terminal,
or if the operating system is perverse) then the functions have to return an
error code to you, so then at least you know.
--
Norman Diamond diamond@jit.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.
Pardon me? Or do I have to commit a crime first?
Technical Details
Message-ID:
<C1DyzH.n1t@jrd.dec.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!decwrl!pa.dec.com!engage.pko.dec.com!e2big.mko.dec.com!jrdzzz.jrd.dec.com!jit533.jit.dec.com!diamondReferences:
<107937@bu.edu>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!sparky!uunet!decwrl!pa.dec.com!engage.pko.dec.com!e2big.mko.dec.com!jrdzzz.jrd.dec.com!jit533.jit.dec.com!diamond From: diamond@jit533.jit.dec.com (Norman Diamond) Subject: Re: C things Message-ID: <C1DyzH.n1t@jrd.dec.com> Sender: usenet@jrd.dec.com (USENET News System) Nntp-Posting-Host: jit533.jit.dec.com Reply-To: diamond@jit.dec.com (Norman Diamond) Organization: Digital Equipment Corporation Japan , Tokyo References: <107937@bu.edu> Date: Mon, 25 Jan 1993 01:33:17 GMT Lines: 65