segmentation fault when using strtok

K

krista

Hi,

I am beginner of C++. When I try to read a file and put it into X and
Y variables. I got that segmentation fault.
the data file is
data.txt:
4.4 6.8
3.2 -5.5
3.3 0.9

I just want to print out the file as target: 4.4,6.8
target: 3.2,
-5.5........

I already can read the file
but i cannot separate the data in a line from the file to x and y
variables

the code is

ifstream in;
in.open(data.txt);
char str[100];
while (!in.eof()){
in>>str;
double X=atof(strtok(str," ");
double Y=atof(strtok(NULL," ");
cout<<"Target:"<< X << " "<< Y <<endl;
}

then the result is segmentation fault.

Please advise.

Krista
 
T

trojanfoe

Hi,

I am beginner of C++. When I try to read a file and put it into X and
Y variables. I got that segmentation fault.
the data file is
data.txt:
4.4  6.8
3.2  -5.5
3.3 0.9

I just want to print out the file as target: 4.4,6.8
                                               target: 3.2,
-5.5........

I already can read the file
but i cannot separate the data in a line from the file to x and y
variables

the code is

ifstream in;
in.open(data.txt);
char str[100];
while (!in.eof()){
in>>str;
double X=atof(strtok(str," ");
double Y=atof(strtok(NULL," ");
cout<<"Target:"<< X << " "<< Y <<endl;

}

then the result is segmentation fault.

Please advise.

Krista

Although I'm not a regular here, I have to say I don't think this is a
C++ question as strtok is part of the C runtime library, not C++
library. Also the program you have posted won't compile (missing
brackets after the atof/strtok combos).

Personally I have hardly ever used strtok, preferring to use sscanf
where I can validate that it returns 2 and perform the whole line
parsing in one shot:

float X, Y;
if (sscanf(line, "%f %f", &X, &Y) != 2)
{
say_something_prickly_to_the_user();
}

Andy
 
V

Vimal Aravindashan

krista said:
I already can read the file
but i cannot separate the data in a line from the file to x and y
variables

the code is

ifstream in;
in.open(data.txt);
char str[100];
while (!in.eof()){
in>>str;

Change the above line to:
in.getline(str, sizeof(str));

The extraction operator stops at the first whitespace, which means, that
'str' does not hold an entire line to be split by strtok().
double X=atof(strtok(str," ");
double Y=atof(strtok(NULL," ");

The second call to strtok() will return NULL, and passing that to atof()
is likely to be the cause for the segmentation fault. It would be wise
to examine the result of the call to strtok() before using it. Like,

char *v;
double X, Y;
v = strtok(str," ");
X = (v == NULL) ? 0 : atof(v);
v = strtok(NULL," ");
Y = (v == NULL) ? 0 : atof(v);

You can of course smash it all up, and make it dense, but I prefer not to.
cout<<"Target:"<< X << " "<< Y <<endl;
}

then the result is segmentation fault.

Please advise.

Krista

Please post code that can be complied without much modification.

--
Regards,
Vi


"If you would be a real seeker after truth, it is necessary that at
least once in your life you doubt, as far as possible, all things."
-- René Descartes
 
V

Vimal Aravindashan

trojanfoe said:
Although I'm not a regular here, I have to say I don't think this is a
C++ question as strtok is part of the C runtime library, not C++
library. Also the program you have posted won't compile (missing
brackets after the atof/strtok combos).
Most C routines are available in C++ as well. Please keep in mind that
it is possible to mix C code in C++:
http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html. Also, the
closest equivalent I can think of for strtok() using C++ is the combo of
string::find_first_of() and string::substr(). Boost does have it's own
tokenizer class: http://www.boost.org/libs/tokenizer/tokenizer.htm.

Besides, it was the OP's use of the stream extraction operator that was
the real issue in the post. I do agree with you on the incomplete code
issue though.
Personally I have hardly ever used strtok, preferring to use sscanf
where I can validate that it returns 2 and perform the whole line
parsing in one shot:

float X, Y;
if (sscanf(line, "%f %f", &X, &Y) != 2)
{
say_something_prickly_to_the_user();
}

Andy

Isn't sscanf() part of C libraries as well? :p

--
Regards,
Vi


"If you would be a real seeker after truth, it is necessary that at
least once in your life you doubt, as far as possible, all things."
-- René Descartes
 
R

red floyd

krista said:
Hi,

I am beginner of C++. When I try to read a file and put it into X and
Y variables. I got that segmentation fault.
the data file is
data.txt:
4.4 6.8
3.2 -5.5
3.3 0.9

I just want to print out the file as target: 4.4,6.8
target: 3.2,
-5.5........

I already can read the file
but i cannot separate the data in a line from the file to x and y
variables

the code is

ifstream in;
in.open(data.txt);
char str[100];

The following loop does not do what you think it does. See FAQ 15.5,
http://parashift.com/c++-faq-lite/input-output.html#faq-15.5
 
J

Jim Langston

krista said:
Hi,

I am beginner of C++. When I try to read a file and put it into X and
Y variables. I got that segmentation fault.
the data file is
data.txt:
4.4 6.8
3.2 -5.5
3.3 0.9

I just want to print out the file as target: 4.4,6.8
target: 3.2,
-5.5........

I already can read the file
but i cannot separate the data in a line from the file to x and y
variables

the code is

ifstream in;
in.open(data.txt);
char str[100];
while (!in.eof()){
in>>str;
double X=atof(strtok(str," ");
double Y=atof(strtok(NULL," ");
cout<<"Target:"<< X << " "<< Y <<endl;
}

then the result is segmentation fault.

You do not need to use strtok to do what you want.

Consider (untested) code:

ifstream in("data.txt");
double X = 0;
double Y = 0;
while ( in >> X >> Y )
{
cout << "Target" << X << " " << Y << endl;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top