字符串的比较与连接——为何将返回值为整型的函数的返回值输出出来,会输出乱码?

img


问:为何用文件输出流 输出的第二行的第一个整型数据,是乱码??

/*字符串的比较与连接,不使用字符串处理函数
两个字符串s1和s2,其长度都不超过30,比较字符串的大小(字符串比较是从左到右逐位比较),
如果s1>s2,输出1;s1=s2,输出0;s1<s2,输出-1。编写函数实现该功能。
int strcmpa(const char *s1,const char *s2)
然后编写函数将两个字符串连接起来
char * strcate(char *s1,const char *s2)
将s2连接到s1之后,并返回s1

如:in.txt
C++ Program
student student
good evening
则:out.txt
-1 C++Program
0 studentstudent
1 goodevening
注意:请勿改动主函数main和其它函数中的任何内容,仅在Begin和End之间填入你编写的若干语句
---------------------------------------------------------------------------------------------------*/
#include<fstream>
using namespace std;
int strcmpa(char *s1,char *s2);
char * strcate(char *s1,char *s2);
int main()
{    ifstream inf("in.txt");
    ofstream outf("out.txt");
    char a[10],b[10];
    int c;
    while(inf>>a>>b)
    {    c=strcmpa(a,b);
        outf<<c<<' '<<strcate(a,b)<<'\n';
    }
    inf.close();
    outf.close();
    return 0;
}
int strcmpa(char *s1,char *s2)
{    while(*s1==*s2)
    {    if(*s1=='\0'&&*s2=='\0')
        {    return 0;
        }
        s1++;
        s2++;
    }
    if(*s1>*s2)
    {    return 1;
    }
    else
    {    return -1;
    }
}
char *strcate(char *s1,char *s2)
{    char *p=s1;
    while(*s1++)
    {
    }
    s1--;
    while(*s2)
    {    *s1=*s2;
        s1++;
        s2++;
    }
    *s1='\0';
    return p;
}



#include<fstream>
using namespace std;
int strcmpa(char* s1, char* s2);
char* strcate(char* s1, char* s2);
int main()
{
    ifstream inf("in.txt");
    ofstream outf("out.txt");
    char a[10] = { 0 }, b[10] = { 0 };
    int c;
    while (inf >> a >> b)
    {
        c = strcmpa(a, b);
        outf << c << ' ' << strcate(a, b) << '\n';
    }
    inf.close();
    outf.close();
    return 0;
}
int strcmpa(char* s1, char* s2)
{
    while (*s1 == *s2)
    {
        if (*s1 == '\0' && *s2 == '\0')
        {
            return 0;
        }
        s1++;
        s2++;
    }
    if (*s1 > *s2)
    {
        return 1;
    }
    else
    {
        return -1;
    }
}
char* strcate(char* s1, char* s2)
{
    char* p = s1;
    while (*s1++)
    {
    }
    s1--;
    while (*s2)
    {
        *s1 = *s2;
        s1++;
        s2++;
    }
    *s1 = '\0';
    return p;
}