Thread: string problems

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    string problems

    im sure the problem is right in front of me, but my functions doesnt seem to work right. its supposed to create a output file name by taking the input's and adding a .html ending to it.

    Code:
    if(output == NULL) {
              spot = strrchr(input, '.');
              
              output = malloc( strlen(spot) + 5 );
              strncpy(output, input, (spot - input -1 )); //-1
              strcat(output, ".html");
    }
    if i run this with the input file being test.txt, i get a output file of tes.txt i tried playing with the numbers, -1 is the closes it gets.

    what completly obvious thing here am i missing?

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    This is a classic.... strncpy doesn't append a nul terminator the way that strcpy does.

    I think that should give you enough to fix the problem.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    This is a classic....
    i knew it had to be something like that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. io stream and string class problems
    By quizteamer in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2005, 12:07 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM