String output, sometimes incorrect

This is a discussion on String output, sometimes incorrect within the C++ Programming forums, part of the General Programming Boards category; In this sample line of code below, the output varies greatly. I would think by the syntax that the number ...

  1. #1
    Unregistered
    Guest

    String output, sometimes incorrect

    In this sample line of code below, the output varies greatly. I would think by the syntax that the number of returned characters should only be 6. But every once in a while the number returned is actually from the starting point in the string (szBarcode) to the end, which is actually 18 characters. How come this line is not functioning properly? What would the next line be if I wanted to take only the first six characters from szfWgt and make that szfWgt?

    strncpy(((char*)wh.szfWgt), (szBarcode + 28),6);

    Thanks in advance for any help.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    strncpy(((char*)wh.szfWgt), (szBarcode + 28),6);

    This should leave you with 6 characters copied. However, it does not insert a zero like strcpy does. So if there hasn't been a zero at wh.szfWgt[6] previously, it won't be there after this line is executed. There are always 6 characters copied, if there are six available. You need to make sure it has a zero at the right position for wh.szfWgt to be an 'sz' again.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Unregistered
    Guest
    Thanks for the quick response. I'm not sure if I entirely understand what you are talking about, but the fact of the matter remains that that command will return as many as 24 characters in the variable szfWgt, not just 6. I really just need to understand how to cut it off so after it has recieved its value (however many numbers that may be), it keeps the first six numbers and deletes the rest. I hope that is clear enough for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM

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