473,608 Members | 2,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems with strtok()

22 New Member
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 3802
arne
315 Recognized Expert Contributor
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
nomad5000
22 New Member
Thank you very much!

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

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

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

Similar topics

2
2057
by: Adam Balgach | last post by:
Hello hello, i am trying to parse up a line that is in the format: @xxxxx yyyyyy {zzzzz, aaaaaa, bbbbbb} where there could be any number of elements inbetween the { ... } i am only concerned with getting the yyyyy and then the tokens in the { }
13
4915
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't happen before 'splitCommands' entered the picture. The problem is in splitCommands( ) somehow modifying the pointer, but I HAVE to call that function. Is there a way to make a copy of it or something ? /* HERE IS MY CODE */ char *...
2
491
by: Michael | last post by:
Hi All, I'm trying to tokenise a string using strtok(), but I cat get it to give me the second token. What have I done wrong here? void function(int *Result, char *Input){ ....variables declared...
8
1921
by: hu | last post by:
hi, everybody! I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0. Program is simple, but mistake is confusing. First, the below code can get right outcome:"ello world, hello dreams." #include <stdafx.h> #include <string.h> #include <stdio.h> int main()
2
1697
by: J4CK4L | last post by:
Hi everyone, I've just created a function in a header file that accept a string and parse but the prototipe of the function return me an exception: #include <stdio.h> #include <string.h> int Scan(char Search) // this generate the exception { // char Search = "Sample text"; //only in this way
4
2724
by: Michael | last post by:
Hi, I have a proble I don't understand when using strtok(). It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok(), the original call is somehow confused or upset. I have the following code, which I am using to tokenise some input which is in th form x:y:1.2: int tokenize_input(Sale *sale, char *string){
29
2563
by: Pietro Cerutti | last post by:
Hello, here I have a strange problem with a real simple strtok example. The program is as follows: ### BEGIN STRTOK ### #include <string.h> #include <stdio.h>
12
2346
by: Pilcrow | last post by:
Here is a quick program, together with its output, that illustrates what I consider to be a deficiency of the standard function strtok from <string.h>: I am using C:\>gcc --version gcc (GCC) 3.4.5 (mingw special) I would like there to be a default, to be returned when two delimiters
14
1696
by: stevenruiz | last post by:
Hello All My question mainly is how to use/reference Double Pointers? I am currently trying to understand what the meaning of a 'vector of pointers' means also? What I am trying to do is take a char array and break it up into words omitting the spaces. What needs to be noted is that I am trying to accomplish this only using char ** and char *. Therefore, I am creating it from scratch. Below is code that I have written so far:
0
8000
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8495
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6815
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.