Like Tree40Likes

strcpy get errors

This is a discussion on strcpy get errors within the C Programming forums, part of the General Programming Boards category; It was used in a useful way in that second program, although Salem pointed out the issue with using fseek ...

  1. #121
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    It was used in a useful way in that second program, although Salem pointed out the issue with using fseek on text streams, so it's basically broken. Since POSIX-compliant systems guarantee that text streams are mapped in the exact same way as binary streams, this isn't really an issue for them, but since POSIX isn't C I suppose it's still an issue within the boundaries of this board. Although, you could still use that example with binary streams quite well, and probably in fewer lines than you would without the call to fseek.

    Otherwise, what's the point? It doesn't help me explain anything practical to a beginner wondering why gets should not be used.
    If you want to give them a good perspective on the issue, encourage them to implement gets for themselves. Then encourage them to implement fgets. That way they'll have a good understanding of the weakness gets has which fgets lacks. They'll also know how to write functions that optimal for their intended use, where fgets usually isn't.

    edit: ... and gets basically always isn't.

  2. #122
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Pfft. That sounds like kid stuff. I use it to write crappy programs that use fseek and stuff.
    I really have no idea what you mean by this. Input is not necessarily a trivial part of a program.

  3. #123
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Using gets to read input you can't make guarantees about is for kids. Real men use gets when they can make such guarantees.

  4. #124
    Registered User
    Join Date
    Jun 2005
    Posts
    6,210
    Quote Originally Posted by Barney McGrew View Post
    I suppose you have a different interpretation of the word 'use' then. For me it's 'wherever gets is called', perhaps 'use' might imply 'used for a useful purpose' for you. I dunno.
    Well .... an English-language dictionary I have at hand defines

    use n. act of putting to a purpose: state or fact of being put to a purpose : advantageous use to which anything may be applied: the fact of serving a purpose: the profit or profitable purpose derived from property: convenience : employment : need : advantage : usefulness.

    For you, the word "use" does not imply a purpose or usefulness, but my dictionary disagrees with you.
    Right 98% of the time, and don't care about the other 3%.

  5. #125
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Code:
    #include <stdio.h>
    
    int main(void) { puts(""); }
    This program does not use puts.

    EDIT: If you think that statement's true, ask around and see how many people agree with you before you explain the ambiguity of the term 'use'.

    EDIT#2: Sorry. Forgot that puts emits a new line with an empty string.

    Code:
    #include <stdio.h>
    
    int main(void) { fputs("", stdout); }
    There we go.
    Last edited by Barney McGrew; 01-18-2013 at 05:44 AM.

  6. #126
    Registered User
    Join Date
    Jun 2005
    Posts
    6,210
    Those programs call puts() and fputs() respectively. They don't call them for any particular purpose (other than, perhaps, your purpose of flogging a dead horse).
    Right 98% of the time, and don't care about the other 3%.

  7. #127
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Nevertheless, many would say fputs is used in those programs. Compare that with the general statement “gets is impossible to use safely”.

    edit: s/those programs/that program/

  8. #128
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    Quote Originally Posted by Click_here
    Strcpy is your best bet with a definition for the maximum string length.
    and strcpy is the best bet for making a single pointer to copy many array, am i right?

    Quote Originally Posted by c99tutorial
    Wow. fgets requires 3 parameters and gets requires 1. Is typing two more parameters really that much extra work?
    hahaha, when i read this i began to laugh much funny say bro,

    Quote Originally Posted by c99tutorial
    Code:
    #define GETS(buf) do { \
         _Static_assert(sizeof buf > sizeof(void*), "GETS(buf): character array required"); \
        fgets(buf, sizeof(buf), stdin); \
         if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0'; \
    } while(0)
    why u using strlen when u define a gets, i didnt quite understand?

    Quote Originally Posted by Barney McGrew
    gets is only as unsafe as strcpy is, in that it will mess up if it's used incorrectly.
    lol so strcpy is bad as gets?
    Then why both of the command is written if it isnt used?
    Its like a trap command for programmer, lol

    Quote Originally Posted by nonpuz
    I was referring to the C11 draft, _NOT_ the microsoft garbage.
    whats ur meaning of microsoft garbage?

    Quote Originally Posted by esbo
    but gets(string) is pretty easy to remember and simple to use.
    lol, gets are using
    Code:
    gets(&string);
    see the &
    is that the programmer forgot to add this,
    and the suck things are error to compile
    and must re-check the code again, like scanf for reading variables

    if gets didnt useful, useless being, crap,
    why the c developer making gets() if it isnt for used?

    thats what all i want to say for the long debating

    i am newbie to C
    i know i didnt know a lot, i confused at some quote but i laugh too for some quote :P
    but i had to learn the basic C code to advanced C code, so if i messing up with gets, puts or other else, it still a lesson.
    It didnt destroy my pc though LoL and i can still fixed the code.
    Soon, if gets() didnt good i will use fgets() if i making a program or vice versa,
    by the way this thread get one thousand audience lol, great!!!
    Last edited by loserone+_+; 01-19-2013 at 04:53 PM. Reason: Error Quote Tags

  9. #129
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    if gets didnt useful, useless being, crap, why the c developer making gets() if it isnt for used?
    thats what all i want to say for the long debating
    gets is from simpler times (think 60s and 70s) when security issues weren't really a huge concern. Only after it was exploited time and again did the lightbulb come on and gets was officially deprecated as well as unofficially discouraged by everyone with any claim to proficiency in C. gets survived up until the most recent standard (C11) for simple fear of breaking existing code. The short answer is just because something exists doesn't mean it's a good idea. There could be legitimate historical reasons that keep a bad idea from the death it deserves.
    My best code is written with the delete key.

  10. #130
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Quote Originally Posted by loserone+_+ View Post
    why u using strlen when u define a gets, i didnt quite understand?
    gets will produce a string without the newline at the end of the string after it reads it. So if you want to emulate gets by using fgets, you must remove the newline from the end if it is present. The following will remove the newline from the end of str if it is present:

    Code:
    if (str[strlen(str)-1] == '\n') str[strlen(str)-1] = '\0';

  11. #131
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    lol so strcpy is bad as gets?
    No. strcpy doesn't care when the destination buffer points to storage that can't contain the source string; it will still proceed to overflow it. gets behaves in the same way when the data read from stdin overflows the destination buffer. It's difficult to ensure that the overflow with gets won't happen, but your call must make that guarantee in order for it to be safe, which is quite difficult (and often impractical) to do in most situations.

    Code:
    gets(&string);
    see the &
    is that the programmer forgot to add this,
    That's much worse. gets requires a char *. You're passing either a char (*)[whatever] or char ** then implicitly converting it to a char * when you do that. gets simply doesn't provide any means for checking the size of the destination storage in order to prevent an overflow. fgets and fread, however, do have that facility.

    EDIT: *scanf, with the 's' format specifier, and I think a few other format specifiers, does too. Although it's not given as a separate parameter, but encoded within the format string.
    laserlight likes this.

  12. #132
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by c99tutorial
    The following will remove the newline from the end of str if it is present:
    Code:
    if (str[strlen(str)-1] == '\n') str[strlen(str)-1] = '\0';
    strchr would do the job better:
    Code:
    char *p = strchr(str, '\n');
    if (p)
    {
        *p = '\0';
    }
    However, Nominal Animal has convinced me that strcspn is more elegant and hardly less efficient:
    Code:
    str[strcspn(str, "\n")] = '\0';
    or if you're paranoid about the '\r' despite the fgets call:
    Code:
    str[strcspn(str, "\r\n")] = '\0';
    Barney McGrew likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #133
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    In practice it's probably a better idea not to simply discard the new line, since you can use it to determine whether an entire line was read. In the case that it wasn't read, you could read the remainder of the line and either handle it or discard it. I believe gets_s (see C11) does the latter automatically, although you'll probably be hard pressed finding a C library which implements that function.

  14. #134
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    However, Nominal Animal has convinced me that strcspn is more elegant and hardly less efficient:
    Well, it is less efficient, and provably so. But since strcspn is my favored solution for removing a newline after fgets, I'd argue that it's silly to optimize the newline removal given that the cost of fgets (being I/O bound) will vastly overwhelm just about any subsequent attempts to sanitize the string.
    Matticus likes this.
    My best code is written with the delete key.

Page 9 of 9 FirstFirst 123456789
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help using strcpy
    By laxkrzy in forum C Programming
    Replies: 1
    Last Post: 11-15-2010, 10:09 PM
  2. Replies: 1
    Last Post: 06-19-2010, 07:42 AM
  3. What's up with this strcpy?
    By fanoliv in forum C Programming
    Replies: 7
    Last Post: 06-19-2006, 05:24 PM
  4. strcpy
    By Tibo in forum C Programming
    Replies: 2
    Last Post: 03-27-2003, 06:02 AM
  5. strcpy
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 08-01-2002, 01:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21