470,815 Members | 2,439 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <stdlib.h>
  5. using namespace std;
  6. void leerMatriz(char * archivo,int matriz[11][11]);
  7. void leerMatriz(char * archivo,int matriz[11][11]){
  8.   ifstream in(archivo,ios::in);
  9.   if(!in){
  10.     cerr<<"no se pudo abrir el archivo "<<archivo<<endl;
  11.     exit(1);
  12.   }
  13.   cout<<"archivo abierto"<<endl;
  14.   char * buffer = new char[20];
  15.   int conta = 0;
  16.   cout<<"matriz[9][9]="<<matriz[0][9]<<endl;
  17.   while(in.getline(buffer,20)&&conta<10){
  18.     cout<<"entrando al while"<<endl;
  19.     char * token = strtok(buffer,",");
  20.     matriz[conta][0]=atoi(token);
  21.     token = strtok(NULL,",");
  22.     matriz[conta][1]=atoi(token);
  23.     token = strtok(NULL,",");
  24.     matriz[conta][2]=atoi(token);
  25.     token = strtok(NULL,",");
  26.     matriz[conta][3]=atoi(token);
  27.     token = strtok(NULL,",");
  28.     matriz[conta][4]=atoi(token);
  29.     token = strtok(NULL,",");
  30.     matriz[conta][5]=atoi(token);
  31.     token = strtok(NULL,",");
  32.     matriz[conta][6]=atoi(token);
  33.     token = strtok(NULL,",");
  34.     matriz[conta][7]=atoi(token);
  35.     token = strtok(NULL,",");
  36.     matriz[conta][8]=atoi(token);
  37.     token = strtok(NULL,",");
  38.     matriz[conta][9]=atoi(token);
  39.     /*token = strtok(NULL,",");*/
  40.     conta++;
  41.     }
  42.   cout<<"matriz leida"<<endl;
  43. }
  44. int main(){
  45.   int matriz[11][11];
  46.   leerMatriz("matriz_ejemplo.txt",matriz);
  47. }
  48.  
the textfile matriz_ejemplo.txt
is the following
Expand|Select|Wrap|Line Numbers
  1. .,.,.,9,.,5,.,.,.
  2. 3,.,.,.,4,6,9,.,.
  3. 7,9,.,.,.,.,.,4,6
  4. 6,.,2,.,3,.,.,.,.
  5. .,.,.,.,.,.,.,.,.
  6. .,.,.,.,6,.,5,.,4
  7. 8,6,.,.,.,.,.,7,9
  8. .,.,1,6,7,.,.,.,3
  9. .,.,.,2,.,9,.,.,.
  10.  
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)
Oct 19 '06 #1
3 3484
arne
315 Expert 100+
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <stdlib.h>
  5. using namespace std;
  6. void leerMatriz(char * archivo,int matriz[11][11]);
  7. void leerMatriz(char * archivo,int matriz[11][11]){
  8.   ifstream in(archivo,ios::in);
  9.   if(!in){
  10.     cerr<<"no se pudo abrir el archivo "<<archivo<<endl;
  11.     exit(1);
  12.   }
  13.   cout<<"archivo abierto"<<endl;
  14.   char * buffer = new char[20];
  15.   int conta = 0;
  16.   cout<<"matriz[9][9]="<<matriz[0][9]<<endl;
  17.   while(in.getline(buffer,20)&&conta<10){
  18.     cout<<"entrando al while"<<endl;
  19.     char * token = strtok(buffer,",");
  20.     matriz[conta][0]=atoi(token);
  21.     token = strtok(NULL,",");
  22.     matriz[conta][1]=atoi(token);
  23.     token = strtok(NULL,",");
  24.     matriz[conta][2]=atoi(token);
  25.     token = strtok(NULL,",");
  26.     matriz[conta][3]=atoi(token);
  27.     token = strtok(NULL,",");
  28.     matriz[conta][4]=atoi(token);
  29.     token = strtok(NULL,",");
  30.     matriz[conta][5]=atoi(token);
  31.     token = strtok(NULL,",");
  32.     matriz[conta][6]=atoi(token);
  33.     token = strtok(NULL,",");
  34.     matriz[conta][7]=atoi(token);
  35.     token = strtok(NULL,",");
  36.     matriz[conta][8]=atoi(token);
  37.     token = strtok(NULL,",");
  38.     matriz[conta][9]=atoi(token);
  39.     /*token = strtok(NULL,",");*/
  40.     conta++;
  41.     }
  42.   cout<<"matriz leida"<<endl;
  43. }
  44. int main(){
  45.   int matriz[11][11];
  46.   leerMatriz("matriz_ejemplo.txt",matriz);
  47. }
  48.  
the textfile matriz_ejemplo.txt
is the following
Expand|Select|Wrap|Line Numbers
  1. .,.,.,9,.,5,.,.,.
  2. 3,.,.,.,4,6,9,.,.
  3. 7,9,.,.,.,.,.,4,6
  4. 6,.,2,.,3,.,.,.,.
  5. .,.,.,.,.,.,.,.,.
  6. .,.,.,.,6,.,5,.,4
  7. 8,6,.,.,.,.,.,7,9
  8. .,.,1,6,7,.,.,.,3
  9. .,.,.,2,.,9,.,.,.
  10.  
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
Expand|Select|Wrap|Line Numbers
  1.   matriz[conta][9]=atoi(token);
  2.  
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).
Oct 20 '06 #2
Thank you very much!

What a stupid mistake.
Oct 20 '06 #3
arne
315 Expert 100+
Thank you very much!

What a stupid mistake.
It was a pleasure to help :)
Oct 20 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by ern | last post: by
2 posts views Thread by Michael | last post: by
2 posts views Thread by J4CK4L | last post: by
4 posts views Thread by Michael | last post: by
29 posts views Thread by Pietro Cerutti | last post: by
12 posts views Thread by Pilcrow | last post: by
14 posts views Thread by stevenruiz | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.