Hi friends,
I 'm trying to extract values from a lines which are delimited by a
space
eg.
content of string is: "Hello World"
I use strtok to extract "Hello" and "World"
the code I use is
foo=atoi(strtok(data," "));
bar=atoi(strtok(NULL," "));
BUT If in case there's only one word, it gives me Segmentation Fault.
How can I handle this?
I tried try-catch but didnt work.
Can anyone please help me with this issue?
Thanks in advance 8 4957
Neel <a.******@gmail.comwrites:
I 'm trying to extract values from a lines which are delimited by a
space
eg.
content of string is: "Hello World"
I use strtok to extract "Hello" and "World"
the code I use is
foo=atoi(strtok(data," "));
bar=atoi(strtok(NULL," "));
BUT If in case there's only one word, it gives me Segmentation Fault.
How can I handle this?
You'll have to show us a small, complete, compilable program that
demonstrates the problem.
I tried try-catch but didnt work.
Are you use a C++ compiler? comp.lang.c++ is down the hall, second
door on the left.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Neel wrote:
Hi friends,
I 'm trying to extract values from a lines which are delimited by a
space
eg.
content of string is: "Hello World"
I use strtok to extract "Hello" and "World"
the code I use is
foo=atoi(strtok(data," "));
bar=atoi(strtok(NULL," "));
BUT If in case there's only one word, it gives me Segmentation Fault.
How can I handle this?
Ever thought of checking the return value of strtok?
I tried try-catch but didnt work.
Well considering C doesn't have try-catch and the descendent language
that does certainly doesn't use them for asynchronous signals, that's
hardly surprising.
--
Ian Collins.
On Sep 26, 3:21*am, Neel <a.******@gmail.comwrote:
Hi friends,
I 'm trying to extract values from a lines which are delimited by a
space
eg.
content of string is: "Hello World"
I use strtok to extract "Hello" and "World"
the code I use is
foo=atoi(strtok(data," "));
bar=atoi(strtok(NULL," "));
BUT If in case there's only one word, it gives me Segmentation Fault.
Sure, if it's only one word it will return NULL on the second call.
How can I handle this?
I tried try-catch but didnt work.
LOL! Did you try something like
try {
foo = atoi(strtok(data, " "));
}
catch (SegmentationFault e) {
printf("I caught a segfault! :-)\n");
}
Sebastian
It gives some error.
Do I need to include any special header file for that?
Neel wrote:
It gives some error.
Do I need to include any special header file for that?
Including the context you are replying to in your posts would be a good
start.
--
Ian Collins.
On Sep 26, 1:21*am, Neel <a.k.v...@gmail.comwrote:
Hi friends,
I 'm trying to extract values from a lines which are delimited by a
space
eg.
content of string is: "Hello World"
I use strtok to extract "Hello" and "World"
the code I use is
foo=atoi(strtok(data," "));
bar=atoi(strtok(NULL," "));
BUT If in case there's only one word, it gives me Segmentation Fault.
How can I handle this?
I tried try-catch but didnt work.
From: http://www.cplusplus.com/reference/c...ng/strtok.html
we have this:
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}
The while loop is due to this:
Return Value
A pointer to the last token found in string.
A null pointer is returned if there are no tokens left to retrieve.
Neel said:
Hi friends,
I 'm trying to extract values from a lines which are delimited by a
space
eg.
content of string is: "Hello World"
I use strtok to extract "Hello" and "World"
the code I use is
foo=atoi(strtok(data," "));
bar=atoi(strtok(NULL," "));
BUT If in case there's only one word, it gives me Segmentation Fault.
How can I handle this?
By taking advantage of the fact that strtok returns NULL if no more tokens
exist.
p = strtok(data, " ");
if(p != NULL)
{
/* okay, got a token, keep going */
foo = strtol(p, &endptr, 10);
if(endptr p)
{
/* okay, foo populated - keep going */
p = strtok(NULL, " ");
if(p != NULL)
{
/* okay, got a token, keep going */
bar = strtol(p, &endptr, 10);
if(endptr p)
{
/* okay, bar populated, keep going */
Neel wrote:
It gives some error.
Do I need to include any special header file for that?
A very special one, I think - it was meant as a joke. The point is that
C doesn't have try/catch. C++ does, and for questions about C++ you
should go to comp.lang.c++. However, I can tell you what their answer
would be: C++ exceptions are also not usable for this purpose.
On the other hand, when strtok() has reached the end of the string you
asked it to tokenize, it returns a null pointer value. You're supposed
to use that fact to decide what to do next. If you try to actually use
that value as if it points to a string (for instance, by passing it to
atoi()), then you're going to get trouble. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: adi |
last post by:
Dear all,
This is more like a theoretical or conceptual question:
which is better, using exception or return code for
a .NET component?
I had created a COM object (using VB6), which uses...
|
by: Noor |
last post by:
please tell the technique of centralize exception handling without try catch
blocks in c#.
|
by: Master of C++ |
last post by:
Hi,
I am an absolute newbie to Exception Handling, and I am trying to
retrofit exception handling to a LOT of C++ code that I've written
earlier. I am just looking for a bare-bones, low-tech...
|
by: tom |
last post by:
Hi,
I am developing a WinForm application and I am looking for a guide on where
to place Exception Handling. My application is designed into three tiers
UI, Business Objects, and Data Access...
|
by: C# Learner |
last post by:
Some time ago, I remember reading a discussion about the strengths and
weaknesses of exception handling. One of the weaknesses that was put
forward was that exception handling is inefficient (in...
|
by: craig |
last post by:
I am wondering if there are some best practices for determining a strategy
for using try/catch blocks within an application.
My current thoughts are:
1. The code the initiates any high-level...
|
by: Paul Brun |
last post by:
Hello all,
Well....I debugged in application further (see int32 to native int thread)
and decided to make a new
post as it is easier for trackability...
Anyway, I am getting a Null Reference...
|
by: George2 |
last post by:
Hello everyone,
Such code segment is used to check whether function call or exception-
handling mechanism runs out of memory first (written by Bjarne),
void perverted()
{
try{
|
by: Ashit Vora |
last post by:
Hi,
I have a quick question...
I have a char* which contains value place:zip
I want to extract both place and zip into some variable say myPlace
and myZip.
I used following functions for...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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,...
|
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...
| |