Substrings and so on

N

Nick Keighley

Thank you! That's a point to start.

so why are you still here?
:)

seriously the answer to the question "how do i do string handling" is
radically different depending on whether you're using C++ or C. C++'s
string handling is far more sophisticated. With C you should probably
acquire (or write!) a decent string handling library. And if you've
got serious serious string handling problems consider a more string
oriented language.


C has no "methods"
or functions to get substrings from a string,
or to take "spaces" ("blanks") away (a typical ["trim"] function)??

no not really

What I exactly need to do is the following:

get hold of a decent tutorial
eg. http://en.wikipedia.org/wiki/The_C_Programming_Language_(book)

and an online reference
eg. http://www.dinkumware.com/manuals/

While there are still new lines:
(1) Get one line from a given text file.
fgets()

(2) In that line, detect a "first" part and a "second part", which are
separated by a "=" symbol.
(3) Take away the possible "blanks" (like a "trim" function would do)
from those parts.
(4) Detect which variable in my program is being referred by the
"first part".
(5) Translate the second part (it is still a "string") into a number.

sscanf() can do most of this (not step 4 though!)

int first;
char second [MAX_SIZE_SECOND];

if (sscanf (line, "%s = %d", &first, second) != 2)
{
/* report error */
}
else
{
/* process stuff */
}

- About #1 : It can be done by means of standard I/O C libraries. I
guess that there are also ways to do it with C++ libraries.

for C++ answers ask in comp.lang.c++ !
- About #2 : [...]
there IS a C standard function
for getting the position of a character (it is "strchr"), but not a
function for substring (unless it is a substring that starts at
position 1, which can be done with "strncpy_s").

don't use strncpy() use strcpy() (or sscanf()!)

[...]
- About #4 : I can do this by using a "case" or an "if" statement.
yes.

No
problem at all with this step, provided that "first part" has been
successfully extracted and trimmed.

- About #5 : I hope that a proper casting statement will be enough.

no casting won't help you. Use sscanf() or strtod() (or one of its
friends)

<snip>
 
N

Nick Keighley

sscanf() can do most of this (not step 4 though!)

   int first;
   char second [MAX_SIZE_SECOND];

   if (sscanf (line, "%s = %d", &first, second) != 2)
   {
       /* report error */
   }
   else
   {
        /* process stuff */
   }

oops!

char first [MAX_LINE_LENGTH];
int second;


if (sscanf (line, "%s = %d", first, &second) != 2)
{
/* report error */
}
else
{
/* process stuff */
}

"line" is of course the previously read input line
 
N

Nick Keighley

I'm going to go out on a limb here and disagree. My first languages were
Perl and Javascript (circa Netscape Navigator 2.0). The problem with
languages that treat strings as _opaque_, discrete objects is that you lose
perspective of so many other important concerns, and can easily develop
horrible habits.

nice post. Interesting and thought provoking.

<snip>
 
K

Keith Thompson

Nick Keighley said:
no casting won't help you. Use sscanf() or strtod() (or one of its
friends)

The problem with using the *scanf() family for numeric conversion is
that the behavior on overflow is undefined. The strto*() functions
don't have this problem.
 
N

Nick Keighley

The problem with using the *scanf() family for numeric conversion is
that the behavior on overflow is undefined.  The strto*() functions
don't have this problem.

yes I forgot that (seems a bit of an oversight- if the library has the
machinary available to detect overflow why not use it in scanf()?). he
seemed like he was parsing a noddy .ini file or some such and I
thought a scanf() solution might be good enough for him.
 
P

Phil Carmody

Lorenzo Villari said:
Even in context I don't understand that one:



"No" I think is opposed to "yes" and that would mean I suppose, you
meant I was saying that what Ersek, Laszlo wrote was wrong, which is
not what I meant and I've explained that in another reply. Am I right?

No was indeed disagreement. However 'that' refers to the most
recently mentioned noun - namely 'comp.lang.c'.

Phil
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top