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: -
#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 -
.,.,.,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)
3 3784 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: -
#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 -
.,.,.,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 -
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).
Thank you very much!
What a stupid mistake.
arne 315
Recognized Expert Contributor
Thank you very much!
What a stupid mistake.
It was a pleasure to help :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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>
|
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)...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |