473,387 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

segmentation fault when using strtok

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
Jan 22 '08 #1
3 4642
On 22 Jan, 05:43, krista <rebat...@gmail.comwrote:
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
Jan 22 '08 #2
krista wrote:
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/in....html#faq-15.5
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
Jan 22 '08 #3
krista wrote:
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;
}

--
Jim Langston
ta*******@rocketmail.com
Jan 24 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Matthew Jakeman | last post by:
can anyone tell me why the following piece of code causes a segmentation fault please ?? char *getFNFromIden(char *ident) { int i = 0 ; printf("identifier %s\n", allMenus.identifier) ;...
10
by: Peter Dragun | last post by:
I am generally new to programming under Unix, but know how to code under Windows. I have to create a simple program, that takes the following information from a file using redirection: 4 4 4 4 4...
13
by: N.S. du Toit | last post by:
Just having a bit of trouble programming with C under FreeBSD 5.1 using the gcc compiler. I'm a bit new to C so my apologies if the answer to my question appear obvious :) Basically I've...
3
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I...
17
by: bofh1234 | last post by:
I need to delimit a string. The delimiters are a semicolon and comma. When I run the program I get a segmentation fault on the first strtok. I followed the examples of others and from my old C...
14
by: Vlad Dogaru | last post by:
Hello, I am trying to learn C, especially pointers. The following code attempts to count the appearences of each word in a text file, but fails invariably with Segmentation Fault. Please help me...
1
by: ziroi | last post by:
Hello! I am new here to the forums and still learning C. I understand your policies on coursework, but I have an error that I've run around and around and can't figure out where my problem is...
10
by: sagitalk | last post by:
Here is my code: #include <iostream> #include <vector> #include <fstream> #include <stdio.h> #include <string> #include <cstring> using namespace std;
4
by: spiralfire | last post by:
I wrote a translator, that reads a DIMACS graph format and writes to a simpler format... basically DIMACS format is: c comment p type nodes edges //type is alwats edge on my problems,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.