Jul 25, 2012 at 12:23pm UTC
Hello,
I'm new in this forum and I'm not a C++ expert. I didn't know how to get data from a .txt and throught internet I found that this could work, but it actually doesn't. I would like to know where is the error. Thank you very much:
#include <iostream>
using namespace std;
#define MAXCODE 5
#define MAXLINE 300
#define MAXDESCRIPTION 100
#define MAXVECTOR 500000
typedef char str_cod[MAXCODE];
typedef char str_des[MAXDESCRIPTION];
typedef char str_line[MAXLINE];
typedef struct type_rute
{
str_cod a_operates;
str_cod a_sells;
str_des alliance;
str_cod origin;
str_cod destination;
str_des measure;
int month_1;
int month_2;
int month_3;
int month_4;
int month_5;
int month_6;
str_cod a_sells_2;
str_des a_cat;
str_des origin_city;
str_des destination_city;
str_des origin_country;
str_des destination_country;
str_des origin_continent;
str_des destination_continent;
};
int main(){
FILE *pArchivo;
type_rute rute[MAXVECTOR];
char *token;
str_line line;
int i=0;
pArchivo = fopen("ALL_Jan10-Jun10-SF-SI.txt","rt");
if(pArchivo==NULL)
{
cout<<"File error."<<endl;
}
else
{
cout<<"File ok."<<endl;
}
while (!feof(pArchivo))
{
fgets(line,MAXLINE,pArchivo);
cout<<"LINE "<<i<<endl;
token = strtok(line,";");
strcpy(rute[i].a_operates,token);
token = strtok(NULL,";");
strcpy(rute[i].a_sells,token);
token = strtok(NULL,";");
strcpy(rute[i].alliance,token);
token = strtok(NULL,";");
strcpy(rute[i].origin,token);
token = strtok(NULL,";");
strcpy(rute[i].destination,token);
token = strtok(NULL,";");
strcpy(rute[i].measure,token);
token = strtok(NULL,";");
rute[i].month_1 = atoi(token);
token = strtok(NULL,";");
rute[i].month_2 = atoi(token);
token = strtok(NULL,";");
rute[i].month_3 = atoi(token);
token = strtok(NULL,";");
rute[i].month_4 = atoi(token);
token = strtok(NULL,";");
rute[i].month_5 = atoi(token);
token = strtok(NULL,";");
rute[i].month_6 = atoi(token);
token = strtok(NULL,";");
strcpy(rute[i].a_sells_2,token);
token = strtok(NULL,";");
strcpy(rute[i].a_cat,token);
token = strtok(NULL,";");
strcpy(rute[i].origin_city,token);
token = strtok(NULL,";");
strcpy(rute[i].destination_city,token);
token = strtok(NULL,";");
strcpy(rute[i].origin_country,token);
token = strtok(NULL,";");
strcpy(rute[i].destination_country,token);
token = strtok(NULL,";");
strcpy(rute[i].origin_continent,token);
token = strtok(NULL,";");
strcpy(rute[i].destination_continent,token);
i++;
}
fclose(pArchivo);
for(i=0;i<3;i++)
{
cout<<"Operating airline: "<<rute[i].a_operates<<endl;
cout<<"Comercializing airline: "<<rute[i].a_sells<<endl;
cout<<"Origin: "<<rute[i].origin<<endl;
cout<<"Destination: "<<rute[i].destination<<endl;
cout<<"Measure: "<<rute[i].measure<<endl;
cout<<"Month 3: "<<rute[i].month_3<<endl<<endl<<endl;
//cout<<setw(8)<<setfill('-')<<"-"<<endl;
}
return 0;
}
Jul 25, 2012 at 12:26pm UTC
What does the error message say?
Jul 25, 2012 at 2:40pm UTC
I tried to run it with another document with much less data and it worked. It may be that it doesn't support so many data but I don't know why? (The document is an excel document in .csv format with 17 columns and almost 500.000 rows)
Thank you very much for trying to help =)
Jul 26, 2012 at 2:19pm UTC
could it be that your input csv file has fields that exceed the max length you have allowed for.
what happens if you use strncpy instead of strpcy?