Thread: strncpy doesn't seem to work

  1. #1
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    strncpy doesn't seem to work

    i had a homework assignment to make a function that does what strncpy does, but my program is supposed to use both my function and the original. well, my function works but the original strncpy won't... any suggestions as to what i might be doing wrong? using dev-cpp 4.9.9.2 windows xp home

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    void mystrcpy(char dst[], const char src[])
    {
        int i = 0;
        while (src[i] != 0)
        {
            dst[i] = src[i];
            i++;
        }
        dst[i] = 0;
    }
    
    void mystrncpy(char dst[], const char src[], int n)
    {
        int i = 0;
        while (i < n)
        {
            if (src[i] != 0)
                dst[i] = src[i];
            i++;
        }
        dst[i] = 0;
    }
    
    int main()
    {
       char s1[] = "Hello there ";
       char result[40];
       char s2[] = "Testing my routine";
       char result2[40];
       char result3[40];
    
       strcpy(result, s1);
       cout << "The output string: " << result << endl;
    
       mystrcpy(result2, s2);
       cout << "My output string: " << result2 << endl;
    
       strncpy(result,s1, 2);
       cout << "The first 2 bytes of the output string: " << result << endl;
    
       mystrncpy(result3,s2, 2);
       cout << "The first 2 bytes of My output string: " << result3 << endl;
    
       cin.get();
       return 0;
    }
    so the part where it uses strncpy and specifies that it should only copy the first two characters is where i'm having the problem. in my output it shows that the whole string has been copied.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    strncpy doesn't add the terminating null character since the count is less than the length of the source string s1. Before you call strncpy, result holds "Hello There " from the call to strcpy that copied s1 into it earlier. Then you copy 'H' and 'e' into the first two positions, but since no null terminator is added the previous string is still there.

    Your mystrncpy adds the null terminator when it shouldn't (if you want it to be the same).

  3. #3
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    ok, i got it to work, i added a new character array which was initialized to all 0's, and so even though strncpy doesn't add the null, it's already there.
    and about my function, i know it's not exactly the same, actually it's a lot different, it doesn't return anything. but i think the main goal was to get it to copy the string to the destination. just to make sure we know how to have the computer copy from one array to the other, and stop after a certain number of elements.
    most of this is the teacher's code... i just filled in the code for the functions pretty much. i made a few changes though, he was using void main() and stuff
    thanks
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Here you go.
    Code:
    #include <iostream>
    #include <ostream>
    
    namespace my
    {
        char *strncpy(char *dest, const char *src, unsigned int n)
        {
            int i = 0;
            while(src[i] != '\0' && i < n)
            {
                dest[i] = src[i];
                i++;
                
            }
            while(i < n)
            {
                dest[i] = '\0';
                i++;
            }
            return dest;
        }
    }
    
    int main()
    {
        char someString[] = "Hello";
        char anotherString [] = "Brian";
        
        std::cout<<someString<<std::endl;
        std::cout<<anotherString<<std::endl;
        
        my::strncpy(someString,anotherString,2);
        
        std::cout<<someString<<std::endl;
        std::cout<<anotherString<<std::endl;
        
        std::cin.get();
        
        return 0;
    }
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM