Why I can’t concatenate to rebuild and digits arrays the content of c char from text file?

the program fails. I want to rebuild the content of a text file eg: a1b2c3 to be like - a1b2c3123, the digits at the end of file

    int counterc=0;
    int counterd=0;
    char rebuild[100];
    char digits[100];
    char c;
    int i;
    FILE *fp;

    fp=fopen("1.txt","r");
    if(fp==NULL)
    {
        printf("\nERROR: File not found for opening\n");
    }
    else
    {
        while(c=fgetc(fp))
        {
            if(c==EOF)
            {
                break;
            }
            if(isdigit(c))
            {
                counterd++;
                strcat(digits,c);
            }
            else
            {
                counterc++;


                strcat(rebuild,c);
            }
        }
    }
    fclose(fp);
    printf("\nNumber of digits: %d ",counterd);
    printf("\nNumber of characters: %d",counterc);

    printf("\nThe content of digits[]: ");

    for( i=0; i<3; i++)
    {
        printf("%s ",digits[i]);
    }
    return 0;
}

if I don't use strcat(rebuild,c) it will printf me the number of characters if I don't use strcat(digits,c) it will printf me the number of digits why I can't put together in the digits array the characters that are digits?



Read more here: https://stackoverflow.com/questions/64524041/why-i-cant-concatenate-to-rebuild-and-digits-arrays-the-content-of-c-char-from

Content Attribution

This content was originally published by Adela Tanca at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: