problems with strtok()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomad5000
    New Member
    • Sep 2006
    • 22

    problems with strtok()

    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:
    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);
    }
    the textfile matriz_ejemplo. txt
    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,.,.,.
    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)
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by nomad5000
    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:
    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);
    }
    the textfile matriz_ejemplo. txt
    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,.,.,.
    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)
    The problem is that you pass NULL to atoi: you have only 9 tokens, but you try to read 10. Remove the line
    Code:
      matriz[conta][9]=atoi(token);
    which is the 10th (not the 9th) and the program will work fine.
    Even better: check the return value of strtok for being NULL (meaning that no token has been found).

    Comment

    • nomad5000
      New Member
      • Sep 2006
      • 22

      #3
      Thank you very much!

      What a stupid mistake.

      Comment

      • arne
        Recognized Expert Contributor
        • Oct 2006
        • 315

        #4
        Originally posted by nomad5000
        Thank you very much!

        What a stupid mistake.
        It was a pleasure to help :)

        Comment

        Working...