Hi everybody!
I'm having trouble using strtok to fill a matrix with int nrs. from a file.
the code that is not working is the following:
the textfile matriz_ejemplo. txt
is the following
the programm compiles fine but when I run it it tells me the following.
$ ./a.exe
archivo abierto
matriz[9][9]=1628583705
entrando al while
entrando al while
21 [main] a 2908 _cygtls::handle _exceptions: Error while dumping state (pro
bably corrupted stack)
Segmentation fault (core dumped)
I'm having trouble using strtok to fill a matrix with int nrs. from a file.
the code that is not working is the following:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
void leerMatriz(char * archivo,int matriz[11][11]);
void leerMatriz(char * archivo,int matriz[11][11]){
ifstream in(archivo,ios::in);
if(!in){
cerr<<"no se pudo abrir el archivo "<<archivo<<endl;
exit(1);
}
cout<<"archivo abierto"<<endl;
char * buffer = new char[20];
int conta = 0;
cout<<"matriz[9][9]="<<matriz[0][9]<<endl;
while(in.getline(buffer,20)&&conta<10){
cout<<"entrando al while"<<endl;
char * token = strtok(buffer,",");
matriz[conta][0]=atoi(token);
token = strtok(NULL,",");
matriz[conta][1]=atoi(token);
token = strtok(NULL,",");
matriz[conta][2]=atoi(token);
token = strtok(NULL,",");
matriz[conta][3]=atoi(token);
token = strtok(NULL,",");
matriz[conta][4]=atoi(token);
token = strtok(NULL,",");
matriz[conta][5]=atoi(token);
token = strtok(NULL,",");
matriz[conta][6]=atoi(token);
token = strtok(NULL,",");
matriz[conta][7]=atoi(token);
token = strtok(NULL,",");
matriz[conta][8]=atoi(token);
token = strtok(NULL,",");
matriz[conta][9]=atoi(token);
/*token = strtok(NULL,",");*/
conta++;
}
cout<<"matriz leida"<<endl;
}
int main(){
int matriz[11][11];
leerMatriz("matriz_ejemplo.txt",matriz);
}
is the following
Code:
.,.,.,9,.,5,.,.,. 3,.,.,.,4,6,9,.,. 7,9,.,.,.,.,.,4,6 6,.,2,.,3,.,.,.,. .,.,.,.,.,.,.,.,. .,.,.,.,6,.,5,.,4 8,6,.,.,.,.,.,7,9 .,.,1,6,7,.,.,.,3 .,.,.,2,.,9,.,.,.
$ ./a.exe
archivo abierto
matriz[9][9]=1628583705
entrando al while
entrando al while
21 [main] a 2908 _cygtls::handle _exceptions: Error while dumping state (pro
bably corrupted stack)
Segmentation fault (core dumped)
Comment