请求一个C++问题

来源:百度知道 编辑:UC知道 时间:2024/09/21 16:45:37
#include<iostream>
using namespace std;

void main()
{
char s1[25],s2[25],s3[25];
freopen("B.in","r",stdin);
cin.getline(s1,27);
cin.getline(s2,27);
cin.getline(s3,27);
cout<<s1<<endl
<<s2<<endl
<<s3<<endl;
}
以上是C++源代码。
显示错误:Run-Time Check Failure #2 - Stack around the variable 's3' was corrupted.
之前我去网上浏览了一些相关,但是还是不明白。所以想来个具体点的,请问怎么做修改?

把s1, s2, s3定义到外面
#include<iostream>
using namespace std;
char s1[25],s2[25],s3[25];
void main()
{

freopen("B.in","r",stdin);
cin.getline(s1,27);
cin.getline(s2,27);
cin.getline(s3,27);
cout<<s1<<endl
<<s2<<endl
<<s3<<endl;
}

我拿这个代码亲自在VC6.0版本中进行编译。编译成功。没有什么错误。
然后,要注意一点:把B.in文件放在该目录下的DEBUG文件夹里和EXE文件放在一起,就可以看到结果了。祝你顺利。