copying the first 10 characters to an array?

This is a discussion on copying the first 10 characters to an array? within the C Programming forums, part of the General Programming Boards category; I have been trying to figure this out fir hours and I can't. I'm trying to write code that if ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    48

    copying the first 10 characters to an array?

    I have been trying to figure this out fir hours and I can't.


    I'm trying to write code that if the user enters a string longer than 10 characters, only the first 10 characters are stored in an array plus the null byte and rest of the characters are ignored. Please give me your suggestions.


    Thank You!

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    strncpy( dest, src, 10 );
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    Code:
    strncpy( dest, src, 10 );
    strncpy() will not add a null byte if there is not one in the first 10 characters of the source string. Thus when using strncpy() one should be careful to always terminate manually.

    It's an unfortunately named function, being that it won't always create a string.

    However, I don't believe this is what the OP was asking. You could do this (code either taken from or adapted from Dan Pop via clc):
    Code:
    char buffer[10 + 1] = "";
    scanf("%10[^\n]%*[^\n]", buffer);
    if (!feof(stdin) && !ferror(stdin)) getchar();
    This will read a line, storing the first ten characters and discarding the rest.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by cas View Post
    strncpy() will not add a null byte if there is not one in the first 10 characters of the source string. Thus when using strncpy() one should be careful to always terminate manually.

    It's an unfortunately named function, being that it won't always create a string.
    Oops, I missed that.
    Just do this then:
    Code:
    strncpy( dest, src, 10 );
    dest[10] = '\0';
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  2. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Replies: 4
    Last Post: 11-18-2001, 06:29 PM

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