为啥最后出现一串烫还有m?另外,原题是要求用上一题程序分别建立文件A和B,我这么写对吗?还是说建立AB要在另一个程序文件里写

//有两个磁盘文件A和B,各存放一行字母,今要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件C中去。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    FILE* fp;
    errno_t err;
    int i = 0, j;
    char c[100], t, ch;
    char str1[100], str2[100];
    if (err = fopen_s(&fp, "test1", "w"))
    {
        printf("cannot open file\n");
        exit(0);
    }
    printf("input a string:\n");
    gets_s(str1);
    while (str1[i] != '!')
    {
        if (str1[i] >= 'a' && str1[i] <= 'z')
            str1[i] -= 32;
        fputc(str1[i], fp);
        i++;
    }
    //fputs(str, fp);
    fclose(fp);
    err = fopen_s(&fp, "test1", "r");
    fgets(str1, strlen(str1) + 1, fp);
    printf("%s\n", str1);
    fclose(fp);
    i = 0;
    if (err = fopen_s(&fp, "test2", "w"))
    {
        printf("cannot open file\n");
        exit(0);
    }
    printf("input a string:\n");
    gets_s(str2);
    while (str2[i] != '!')
    {
        if (str2[i] >= 'a' && str2[i] <= 'z')
            str2[i] -= 32;
        fputc(str2[i], fp);
        i++;
    }
    //fputs(str, fp);
    fclose(fp);
    err = fopen_s(&fp, "test2", "r");
    fgets(str2, strlen(str2) + 1, fp);
    printf("%s\n", str2);
    fclose(fp);

    if (err = fopen_s(&fp, "test1", "r"))
    {
        printf("cannot open the file1!\n");
        exit(0);
    }
    printf("file A:\n");
    for (i = 0; (ch = fgetc(fp)) != EOF; i++)
    {
        c[i] = ch;
        putchar(c[i]);
    }
    printf("\n");
    fclose(fp);
    if (err = fopen_s(&fp, "test2", "r"))
    {
        printf("cannot open the file2!\n");
        exit(0);
    }
    printf("file B:\n");
    for (;( ch = fgetc(fp))!=EOF; i++)
    {
        c[i] = ch;
        putchar(c[i]);
    }
    printf("\n");
    fclose(fp);
    for(i=0;i<strlen(c);i++)
        for (j = i + 1; j < strlen(c); j++)
        {
            if (c[i] > c[j])
            {
                t = c[i];
                c[i] = c[j];
                c[j] = t;
            }
        }
    printf("file C:\n");
    err = fopen_s(&fp, "text3", "w");
    for (i = 0; i < strlen(c); i++)
    {
        fputc(c[i], fp);
        putchar(c[i]);
    }
    printf("\n");
    fclose(fp);
    return 0;
}

img


为啥最后出现一串烫还有m?另外,原题是要求用上一题程序分别建立文件A和B,我这么写对吗?还是说建立AB要在另一个程序文件里写?求解

在拷贝file B循环结束后,加上字符串终止符c[i] = '\0';