Thread: Why is strcat() cutting off my string?

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    40

    Why is strcat() cutting off my string?

    Hi Guys, I'm trying to understand why one way my printf works as expected and depending on order of code, it doesn't. BTW: I'm new to C.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void main()
    {
      char fpath1[] = "C:/work/arduino/teraterm-4.106/logs/";
      char fpath2[] = "C:/work/arduino/teraterm-4.106/logs/";
      char fname1[] = "lora-meter.log.1";
      char fname2[] = "lora-meter.log.2";
      strcat(fpath1, fname1);
      // printf("\nfpath1 = %s", fpath1);    // entire path prints fine
      strcat(fpath2, fname2);
      printf("\nfpath1 = %s", fpath1);  // most of path and file is cut off
      printf("\nfpath2 = %s", fpath2);
    }
    Something about the 2nd "strcat(fpath2, fname2);" is trumping what the first "strcat(fpath1, fname1);" is setting up. How can I fix this?
    Last edited by Salem; 08-31-2022 at 07:29 PM. Reason: removed crayola

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    First, main returns int, not void.

    Second, fpath1 is not large enough to append anything to it. Make it larger:

    Code:
    char fpath1[1000] = "C:/work/arduino/teraterm-4.106/logs/";
    Do the same with fpath2.

    It doesn't have to be 1000 but has to be big enough to hold a concatenated string of fpath2 plus fname1.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    You have not left enough room in fpath1 and fpath2 to concatenate further characters to the strings. If you leave the brackets blank then the size of the char array is determined by the initializing string.
    Code:
    char s[] = "hello"; // s only has room for 6 chars: 'h','e','l','l','o','\0'
    char s[100] = "hello"; // s has room for 100 chars
    In your program, the first strcat and printf seem to work because the extra chars are written beyond the actual end of the char array, which is a serious runtime error but is not generally caught by the system.
    fname1 and fname2 could just be const char pointers instead of char arrays.
    BTW, to be more portable main should return int. Only some systems allow void.
    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main(){
        char fpath1[100] = "C:/work/arduino/teraterm-4.106/logs/";
        char fpath2[100] = "C:/work/arduino/teraterm-4.106/logs/";
        const char *fname1 = "lora-meter.log.1";
        const char *fname2 = "lora-meter.log.2";
        strcat(fpath1, fname1);
        strcat(fpath2, fname2);
        printf("fpath1 = %s\n", fpath1);
        printf("fpath2 = %s\n", fpath2);
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Aug 2022
    Posts
    40

    Thank you for the answer and the insight.

    Wow, thank you so much for the reason and not just the solution. Of course, the solution was awesome as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string value disappearing after strcat
    By gephenzie in forum C Programming
    Replies: 2
    Last Post: 12-03-2021, 03:38 PM
  2. cutting a string
    By keeper in forum C++ Programming
    Replies: 2
    Last Post: 05-24-2006, 06:43 PM
  3. Cutting up an std::string
    By Shamino in forum C++ Programming
    Replies: 26
    Last Post: 04-04-2006, 11:02 AM
  4. Question on string concatenate (strcat)...
    By cjmdjm in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2005, 12:42 AM
  5. Removing 'strcat' ed string.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-14-2001, 12:09 AM

Tags for this Thread