Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Substrings and so on

Reply
Thread Tools

Substrings and so on

 
 
Nick Keighley
Guest
Posts: n/a
 
      01-27-2010
On 26 Jan, 17:02, Vicent <(E-Mail Removed)> wrote:
> On 26 ene, 15:20, (E-Mail Removed) (Ersek, Laszlo) wrote:
> > In article <(E-Mail Removed)>, Vicent <(E-Mail Removed)> writes:



> > For C++, I guess you'd look first at std::string and std:iostream.

>
> 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.

<snip>

> > > I've read some tutorials that deal with the standard C I/O and string
> > > (string.h) libraries, but specially when managing strings, I am a bit
> > > lost: Are there methods


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

<snip>

> What I exactly need to do is the following:


get hold of a decent tutorial
eg. http://en.wikipedia.org/wiki/The_C_P...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>
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      01-27-2010
On 27 Jan, 09:08, Nick Keighley <(E-Mail Removed)>
wrote:
> 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

 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      01-27-2010
On 26 Jan, 21:32, William Ahern <will...@wilbur.25thandClement.com>
wrote:
> On 26 Jan 2010 15:20:07 +0100 (E-Mail Removed) (Ersek, Laszlo) wrote:
>
> > For C: don't start with it. Low-level string manipulation is one of
> > the most error-prone tasks in general, leading to countless security
> > vulnerabilities.

>
> 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>
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-27-2010
Nick Keighley <(E-Mail Removed)> writes:
[...]
> 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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      01-28-2010
Lorenzo Villari <(E-Mail Removed)> writes:
> On 26 Jan 2010 15:20:07 +0100
> (E-Mail Removed) (Ersek, Laszlo) wrote:
>
>>
>> For C: don't start with it. Low-level string manipulation is one of
>> the most error-prone tasks in general, leading to countless security
>> vulnerabilities.

>
> I thought that was comp.lang.c...


No, that leads to countless insecurities instead.

Phil
--
Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      01-28-2010
On 27 Jan, 16:52, Keith Thompson <(E-Mail Removed)> wrote:
> Nick Keighley <(E-Mail Removed)> writes:


> > 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.


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.

 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      01-28-2010
Lorenzo Villari <(E-Mail Removed)> writes:
> On Thu, 28 Jan 2010 11:39:57 +0200
> Phil Carmody <(E-Mail Removed)> wrote:
>
>>
>> No, that leads to countless insecurities instead.

>
> No what?


If you cast off all context so quickly, you will often not understand.

Phil
--
Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      01-30-2010
Lorenzo Villari <(E-Mail Removed)> writes:
> On Thu, 28 Jan 2010 20:59:46 +0200
> Phil Carmody <(E-Mail Removed)> wrote:
>
>> If you cast off all context so quickly, you will often not understand.

>
> Even in context I don't understand that one:
>
> On Thu, 28 Jan 2010 11:39:57 +0200
> Phil Carmody <(E-Mail Removed)> wrote:
>
>> Lorenzo Villari <(E-Mail Removed)> writes:
>> > On 26 Jan 2010 15:20:07 +0100
>> > (E-Mail Removed) (Ersek, Laszlo) wrote:
>> >
>> >>
>> >> For C: don't start with it. Low-level string manipulation is one of
>> >> the most error-prone tasks in general, leading to countless
>> >> security vulnerabilities.
>> >
>> > I thought that was comp.lang.c...

>>
>> No, that leads to countless insecurities instead.

>
> "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
--
Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Again substrings and so on Vicent Giner-Bosch C++ 3 01-26-2010 06:16 PM
Removing duplicates and substrings from an array Sam Larbi Ruby 10 11-28-2007 10:32 PM
Finding and Replacing Substrings In A String DarthBob88 C Programming 7 09-23-2007 03:14 PM
Char strings, pointers and substrings. Lawrie C Programming 8 04-07-2005 06:38 PM
Binary files, substrings and (un)packing. Leandro Pardini Perl 1 10-27-2003 07:57 PM



Advertisments