-2
我有我从阅读并包含以下内容的文本文件:C++字符串长度错误异常
file "transform.in" .....:
10
@[email protected]
----------
----------
----------
----------
----------
----------
----------
----------
----------
@---------
----------
----------
----------
----------
----------
----------
----------
----------
[email protected]
(end of file)
,我使用下面的代码创建一个segmation故障:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
ifstream input("transform.in");
ofstream output("transform.out");
int n;
char arxiko[10][10] = {0};
string teliko = "", line;
int main()
{
input >> n;
for (int i=0; i<n; i++)
input >> arxiko[i];
for (int i=0; i<n; i++)
{
input >> line;
teliko += line; // here, in the first iteration the segmation fault occurs
}
如果一行包含9个字符而不是10个,没有问题,但是有10个崩溃! 此外,我做了一些与字符串+ =运算符的测试,我可以追加一个大于10个字符的字符串成功,但为什么我在这失败?
10个字符(终止为0)会生成11个字节。 –
你为什么要一起使用'char **'以及'std :: string'? – CinCout
@gargankit,因为这是产生错误的代码段。在剩下的程序中,我需要char ** - 字符串有两个索引值(arxiko [i] [j])。 – manolismi