我要解决以下任务:MVS C++错误:串标超出范围
有被赋予一个文本文件“pesel.txt”,其中包含150所国家认同。每行包含一个国家标识,这是一个11位数字编号。前两位数字从左边开始确定年份,一个人出生在哪一年,后两位数字决定月份,下两个决定日期。
为了缩短:0-1 =年
数字 位2-3 =月 数字4-5 =天 位6-11 =确定别的东西,是什么并不重要
我需要阅读这个文件,检查有多少人在十二月出生。我想这以下列方式:
- 读取每一行直至到达文件末尾
- 在每一行我检查字符串中的第三个字符是否等于1,如果第四个字符等于2,如果是我增加变量,这是我出生在十二月的人反,否则在下一个循环中执行
这里是代码:
int _tmain(int argc, _TCHAR* argv[])
{
ifstream file("C:\\Kuba\\Studia & Nauka\\MATURA XDDD
\\INFA\\1\\Dane_PR\\pesel.txt");
string line;
int bornInDecember=0;
if(!file.is_open()){
cout << "Cannot read the file." << endl ;
}else{
while(file.good()){
getline(file, line);
if( line[2] == '1' && line[3] == '2' ){
bornInDecember++ ; // 0-1 year, 2-3 month, 4-5 day
}
}
cout << "Amount of people born in december : "<< bornInDecember<< endl;
file.close();
}
system("pause");
return 0;
}
的问题是,我出现以下错误和我不知道为什么..
http://img10.imageshack.us/i/mvserr.png/
行是空的,或者您正在访问不存在的数据。 – DumbCoder 2011-03-28 10:58:18