读书人

c++中函数报错说out of range不知道

发布时间: 2012-07-03 13:37:43 作者: rapoo

c++中函数报错说out of range,不知道为啥,我个人认为是vector或者string上出现了问题,请高手解答。
#include <vector>
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <conio.h>
#define N 100

using namespace std;

int main()
{
vector <string> str;
string line;
string num[N];
string nom[N];
//string an[N];
string temp[N];
ofstream absent("absent.xls"); // le fichier pour ecrire

char data[10];
int i,nlen=8;

string now[100];
string code[100];
time_t ltime;

//entree le numero de codebarre, et noter le numero et le temps
//*************************************************************************
for(int j=0;j<2;j++)
{
for(i=0;i<nlen;i++)
{
data[i]=getch();
putchar(data[i]);
}
data[i]=0;
cout<<endl;
code[j] = string(data);
cout<<endl;
time(& ltime);
now[j]=ctime(& ltime);
int tlen=now[j].length();
string tmp(now[j],0,tlen-1);
now[j]=tmp;
}
//***************************************************************************************
char txtname[3][10] ={"1A.txt","2A.txt"};
for(int k=0;k<2;k++)
{
ifstream ifile( txtname[k]); //le fichier pour lire
while (!ifile.eof())
{
getline(ifile, line); //lire une ligne
str.push_back(line); //inserer le vector
}

//cout <<endl;
int len=str.size();
ifile.clear();
ifile.close();
//*******************************************************************************
char tmp[15]="out";
strcat( tmp , txtname[k] );
strcpy( txtname[k], tmp );
cout<<txtname[k]<<endl;
ofstream outfile( txtname[k]); // le fichier pour ecrire
for(i=0; i <len; i++)
{
string tmp1(str[i],0,nlen);
//temp[i]=""; //vide le l'heure
//num[i]=tmp1;
//outfile<<num[i]<<"";
string tmp2;
tmp2.assign(str[i],nlen+1,string::npos);
nom[i]=tmp2;
outfile<<nom[i]<<"";

}

}
return 0;
}

[解决办法]
data[i]=0;//改成'\0'
cout<<endl;
[解决办法]
另外,楼主确认是想把1A.txt输出给out1A.txt,而把1A.txt+2A.txt输出给out2A.txt吗?
如果不是,需要
char txtname[3][10] ={"1A.txt","2A.txt"};
for(int k=0;k<2;k++)
{
str.clear(); <--- 清空str
...
[解决办法]
《高质量C/C++编程指南》
下来看下

读书人网 >C++

热点推荐