Problems with strtol() from <stdlib.h>
Author |
Message |
Bernd Schust #1 / 5
|
 Problems with strtol() from <stdlib.h>
Hello, I want to convert a string into a long int. The string of numbers is given as following: long a; char inword[256] fscanf( Datei1, "%s", inword); For strtol I need refering to the reference the following syntax a = strtol(char* source; char** end, int basis); In my cas it's a = strtol(inword,??,10) How do I get char** end??? Is there a simplier way for doing this? Thank you in advance, Bernd
|
Mon, 02 May 2005 00:18:22 GMT |
|
 |
Joona I Palast #2 / 5
|
 Problems with strtol() from <stdlib.h>
Quote: > Hello, > I want to convert a string into a long int. > The string of numbers is given as following: > long a; > char inword[256] > fscanf( Datei1, "%s", inword); > For strtol I need refering to the reference the following syntax > a = strtol(char* source; char** end, int basis); > In my cas it's > a = strtol(inword,??,10) > How do I get char** end??? > Is there a simplier way for doing this?
The char** end parameter is a pointer to a char* that will store the location where the conversion ended. You are supposed to already have a suitable char* and pass its address to char** end. If you do not need the char** end functionality and wish not to use it simply pass a NULL pointer (please, regulars, no massive arguments about the spelling, thanks) as the char** end parameter. --
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++| | http://www.helsinki.fi/~palaste W++ B OP+ | \----------------------------------------- Finland rules! ------------/ "The truth is out there, man! Way out there!" - Professor Ashfield
|
Mon, 02 May 2005 00:31:08 GMT |
|
 |
Zoran Cutur #3 / 5
|
 Problems with strtol() from <stdlib.h>
Quote: > Hello, > I want to convert a string into a long int. > The string of numbers is given as following: > long a; > char inword[256] > fscanf( Datei1, "%s", inword); > For strtol I need refering to the reference the following syntax > a = strtol(char* source; char** end, int basis); > In my cas it's > a = strtol(inword,??,10) > How do I get char** end???
char *endp; a = strtol(inword, &endp, 0); --
"LISP is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days." -- Eric S. Raymond
|
Mon, 02 May 2005 01:13:09 GMT |
|
 |
nrk #4 / 5
|
 Problems with strtol() from <stdlib.h>
Quote:
> Hello, > I want to convert a string into a long int. > The string of numbers is given as following: > long a; > char inword[256] > fscanf( Datei1, "%s", inword); > For strtol I need refering to the reference the following syntax > a = strtol(char* source; char** end, int basis); > In my cas it's > a = strtol(inword,??,10) > How do I get char** end???
end is set by strtol to point to the place where it stopped parsing the input string (very handy if you are parsing a long line containing several things and need to track where you are). If you are not interested in keeping track of how much of source was successfully parsed by strtol, just pass NULL. Otherwise, declare a char * variable and pass its address. Whatever "reference" you are using should give you atleast this basic information about strtol. -nrk.
|
Mon, 02 May 2005 03:04:30 GMT |
|
 |
those who know me have no need of my nam #5 / 5
|
 Problems with strtol() from <stdlib.h>
in comp.lang.c i read: Quote: >I want to convert a string into a long int. >The string of numbers is given as following: >long a; >char inword[256] >fscanf( Datei1, "%s", inword);
construct the fscanf safely, e.g., if (1 != fscanf( Datei1, "%255s", inword )) /* 255 = sizeof inword - 1 */ { /* cope with problem, e.g., */ if (ferror(Datei1)) abort(); /* abort on file handling error */ inword[0] = '\0'; /* else make inword a zero length string */ } -- bringing you boring signatures for 17 years
|
Mon, 02 May 2005 05:51:17 GMT |
|
|
|