home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / std / c / 3410 < prev    next >
Encoding:
Text File  |  1993-01-24  |  3.6 KB  |  79 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!decwrl!pa.dec.com!engage.pko.dec.com!e2big.mko.dec.com!jrdzzz.jrd.dec.com!jit533.jit.dec.com!diamond
  3. From: diamond@jit533.jit.dec.com (Norman Diamond)
  4. Subject: Re: C things
  5. Message-ID: <C1DyzH.n1t@jrd.dec.com>
  6. Sender: usenet@jrd.dec.com (USENET News System)
  7. Nntp-Posting-Host: jit533.jit.dec.com
  8. Reply-To: diamond@jit.dec.com (Norman Diamond)
  9. Organization: Digital Equipment Corporation Japan , Tokyo
  10. References: <107937@bu.edu>
  11. Date: Mon, 25 Jan 1993 01:33:17 GMT
  12. Lines: 65
  13.  
  14. In article <107937@bu.edu> spacefox@acs.bu.edu (Godfrey Degamo) writes:
  15.  
  16. Here we go again, comp.std-and-homework-server.c.  These issues are no
  17. different in the C standard than they were in the language 15 years ago.
  18. Aren't there any tutors at BU that you can ask to help teach you?
  19.  
  20. >     I would like to know why it's "dangerous" to use gets().  I always
  21. >make certain I pass in the name of a char array or some pointer char that
  22. >has been allocated some memory.  It always seems to work for me.
  23.  
  24. It worked for a program called "fingerd" as well.  "fingerd" passed a pointer
  25. to a pointer char that had been allocated some memory.  And read characters,
  26. and read, and read, and read, and read, and kept reading.  It read enough
  27. characters to step on its own run-time stack and execute a virus that it
  28. happily read in.
  29.  
  30. Of course, no one's likely to type a virus as input to your program.  But
  31. when they type more characters that you've allocated, your program likely
  32. won't work as well for them as it works for you.
  33.  
  34. >     What other alternative is there for one to use?
  35.  
  36. The documentation of fscanf() is very close to the documentation of scanf().
  37. Read it and notice what the differences are.  One difference is important here.
  38.  
  39. >I seem to have problems with scanf.  For instance:
  40. >     scanf ("%s", &inputA);
  41. >     scanf ("%s", %inputB);
  42. >     What happens is that inputA gets the user input, then when the computer
  43. >comes to the second line, it reads the CR from the first input as the input
  44. >for inputB.
  45.  
  46. Exactly as documented.
  47.  
  48. >(I don't remember this: but, I think that I tried a variation
  49. >"%s\r" and it still wouldn't work.)
  50.  
  51. The scanf() function (and most input functions, in fact all input functions
  52. while operating in an ANSI/ISO standard C environment) never see \r.  They
  53. see \n.  The operating system usually accepts \r from the keyboard in order
  54. to be friendly to the user, but sends \n to the program.  Anyway, if you want
  55. to read just one character and throw it away, there is a function that will
  56. read one character, and there is a format code to scanf() that will read one
  57. character, and you don't even have to care which character it is.
  58.  
  59. >     How do I modify then save the records of one person without loading up
  60. >all the data then saving all the data
  61.  
  62. If you know where the record is, use fsetpos().  If you don't know where the
  63. record is, you have to read until you find it.  Maybe when you first create
  64. the file, you should write some information to another file to help you
  65. remember later where each record was, but the standard doesn't provide any
  66. help for this.
  67.  
  68. >the concept of Random Access Files and Sequential Files
  69.  
  70. In standard C, you can always access a file sequentially, and you can always
  71. try random access by calling fsetpos(), fgetpos(), etc.  If there is some
  72. reason why random access functions won't work (e.g. if the file is a terminal,
  73. or if the operating system is perverse) then the functions have to return an
  74. error code to you, so then at least you know.
  75. --
  76. Norman Diamond                diamond@jit.dec.com
  77. If this were the company's opinion, I wouldn't be allowed to post it.
  78. Pardon me?  Or do I have to commit a crime first?
  79.