473,749 Members | 2,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.tx t);
char str[100];
while (!in.eof()){
in>>str;
double X=atof(strtok(s tr," ");
double Y=atof(strtok(N ULL," ");
cout<<"Target:" << X << " "<< Y <<endl;
}

then the result is segmentation fault.

Please advise.

Krista
Jan 22 '08 #1
3 4671
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.tx t);
char str[100];
while (!in.eof()){
in>>str;
double X=atof(strtok(s tr," ");
double Y=atof(strtok(N ULL," ");
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_p rickly_to_the_u ser();
}

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.tx t);
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(s tr," ");
double Y=atof(strtok(N ULL," ");
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.tx t);
char str[100];
while (!in.eof()){
in>>str;
double X=atof(strtok(s tr," ");
double Y=atof(strtok(N ULL," ");
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*******@rocke tmail.com
Jan 24 '08 #4

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

Similar topics

15
7238
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) ; while(allMenus.identifier != '\0') { printf("Hello World") ;
10
7234
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 4 5 5 5 5 5 5 3 3 3 3 3 3 3 3 2 2 2 10 10 10 1 1 3 3 3 3 3 3 3 3 3 2 2 2 12 12 12 1 7 7 7 7 4 12 13 13 5 5 5 3 3 9 7 7 7 5 4 4 4 3 3 3 3 read each line, and "compress the information" to the following out print to be printed on screen:
13
22168
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 written a function that will check whether a string is an ip address (see the function isIP below). During my attempt at debugging this problem I inserted a printf statement before the return command from the statement, and also a printf statement...
3
2880
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 call the function using this code fragement in my main function: --- char** arg_array; arg_count = create_arg_array(command, argument, arg_array); for(count = 0; count < arg_count; count++)
17
5208
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 books, but I can't seem to find the problem. The accesslist has a format of 20,45;22,44;46,28;99,43,etc. What am I doing wrong? Thanks, #include <sys/signal.h> #include <messages.h>
14
3200
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 out, I've already tried all my ideas. Also, please do comment on my coding style or other aspects. Thank you. #include <stdio.h> #include <string.h>
1
4464
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 with my assignment. I hope you guys can point me in the right direction. I have a program which has an array of linked lists, with specific struct data elements inside each node of these lists. The linked lists are created dynamically and data...
10
4160
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
8086
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, nodes is the number of nodes and edges number of edges e v1 v2 //this means an edge connecting v1 and v2, both integers
0
8997
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
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
9568
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...
0
9389
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6079
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();...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
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
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.