Like Tree1Likes
  • 1 Post By laserlight

Breaking strcpy strncpy fgets down for dummies (me) Please break this down for me

This is a discussion on Breaking strcpy strncpy fgets down for dummies (me) Please break this down for me within the C Programming forums, part of the General Programming Boards category; strcpy vs strncpy? Pros / cons?? Code: pinput = fgets(input, 30, stdin); strncpy(my_struct.firstName,pinput,30); fgets(input["I understand"],30["I understand this"], stdin["I don't understand ...

  1. #1
    Registered User Alexander87's Avatar
    Join Date
    Mar 2013
    Location
    Jacksonville, Florida, United States
    Posts
    11

    Breaking strcpy strncpy fgets down for dummies (me) Please break this down for me

    strcpy vs strncpy? Pros / cons??

    Code:
       pinput = fgets(input, 30, stdin);
        strncpy(my_struct.firstName,pinput,30);
    fgets(input["I understand"],30["I understand this"], stdin["I don't understand why it needs to be here or what else may or may not take its place"])

    strncpy(my_struct.firstname[I understand],pinput[understand],30[makes sense to me])
    however
    Code:
        pinput = fgets(input, buff, stdin);
        strncpy((*cl+pclientCounter), pinput, strlen(pinput) - 1)

    In this piece of code i am getting confused because of strlen(pinput) -1 ? and why does it require a counter for the struct pointer...
    I know you guys don't see the whole code My question isn't about specifically why in this particular code but more so in a general sense. Perhaps if someone could provide a few better examples of how to use these functions properly?

    I would be extremely grateful if someone took the time to break these few things down for me. Thanks ahead of time.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    From what you understand now, what is fgets used for? What is strncpy used for? Go into the details that you know.
    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

  3. #3
    Registered User Alexander87's Avatar
    Join Date
    Mar 2013
    Location
    Jacksonville, Florida, United States
    Posts
    11
    fgets accepts arguments and you can specify length and strncpy/strcpy - since C makes you do arrays for string it makes it easier to do a string instead of filling
    Code:
     char string[3] = {J,o,e};

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Alexander87
    fgets accepts arguments and you can specify length
    If someone from some lost jungle tribe pointed to a car and asked you what it was used for, would you say "it has a window that you can wind down"? Even if you cannot drive, you would probably say something like "it is used to move people from one place to another".

    Now, I'm asking you something similiar. What is fgets used for? You don't to get it completely right (otherwise you would not need to ask about it ), but this would be a starting point from which we can clarify.

    Quote Originally Posted by Alexander87
    strncpy/strcpy - since C makes you do arrays for string it makes it easier to do a string instead of filling
    Hmm... okay. What's the difference between strncpy and strcpy?
    AndiPersti 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

  5. #5
    Registered User Alexander87's Avatar
    Join Date
    Mar 2013
    Location
    Jacksonville, Florida, United States
    Posts
    11
    lol you're like a Professor testing me on something I didn't study for =). I really don't know the difference but your question made me look up just that. It basically said that strn was added later and that it ought to be used. Which isn't really the best answer, so I will dig deeper and get back to you... As for the fgets I thought I answered that with my pathetic knowledge of the subject matter lol but I will i suppose look deeper and get back to you on that as well =). Thank you Laserlight, once I find out more perhaps you can fill in the blanks with your seemingly extensive knowledge/experience.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C how to make a strncpy with fgets and pointers
    By Alexander87 in forum C Programming
    Replies: 5
    Last Post: 03-31-2013, 09:42 PM
  2. Help with fgets and strcpy - returning 3!
    By twizzle in forum C Programming
    Replies: 6
    Last Post: 02-03-2013, 05:48 PM
  3. strcpy and strncpy
    By roaan in forum C Programming
    Replies: 5
    Last Post: 07-26-2009, 08:04 PM
  4. fgets and breaking out of while loop
    By pollypocket4eva in forum C Programming
    Replies: 25
    Last Post: 01-05-2009, 05:50 PM
  5. fgets, fflush, strcpy...
    By blanny in forum C Programming
    Replies: 4
    Last Post: 05-11-2004, 04:14 PM

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