Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Substrings and so on

 
 
Vicent
Guest
Posts: n/a
 
      01-26-2010
I posted this also in comp.lang.c++, so sorry for multiposting.

I would like to ask you about standard or usual ways to manage with
files or strings, specially when getting input data and writing output
from an algorithm.

I mean: which structures, data types or classes? Which standard ways
to read/write from/on files?

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 or functions to get substrings from a string,
or to take "spaces" ("blanks") away (a typical "wrap" function)??

About reading data from a text file, I think this is called "parsing".
Is there any "parsing" library???

Sorry if my questions are too naive, but I am a beginner.

Thank you very much in advance!

--
Vicent
 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      01-26-2010
On Jan 26, 6:40*am, Vicent <(E-Mail Removed)> wrote:
> I posted this also in comp.lang.c++, so sorry for multiposting.
>
> I would like to ask you about standard or usual ways to manage with
> files or strings, specially when getting input data and writing output
> from an algorithm.
>
> I mean: which structures, data types or classes? Which standard ways
> to read/write from/on files?
>
> 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 or functions to get substrings from a string,
> or to take "spaces" ("blanks") away (a typical "wrap" function)??
>
> About reading data from a text file, I think this is called "parsing".
> Is there any "parsing" library???
>
> Sorry if my questions are too naive, but I am a beginner.
>
> Thank you very much in advance!


Sounds more like you have a comp.sci problem than a C problem, as in
learn how to manipulate data first then pick a language to express it.

Also, pick a single language and go with it. There is no C/C++ or
whatever. If you want to learn how to manipulate strings in C++
that's fine but at what I'm guessing is your level I'd stick to one or
another, specially since they're not related.

Tom
 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      01-26-2010
Vicent wrote:
> I posted this also in comp.lang.c++, so sorry for multiposting.


If you are using C++ then I believe C++ provides a lot more facilities
string facilities than C.

> I would like to ask you about standard or usual ways to manage with
> files or strings, specially when getting input data and writing output
> from an algorithm.
>
> I mean: which structures, data types or classes? Which standard ways
> to read/write from/on files?


C only has one string data structure (which is not a type), and that is
the nul terminated string. For input/output you have the functions in
stdio.h

A number of people have string libraries which they have written
themselves, which use more complex structures. However, these are not
standard.

> I've read some tutorials that deal with the standard C I/O and string
> (string.h) libraries,


That's what you get.

> but specially when managing strings, I am a bit
> lost: Are there methods or functions to get substrings from a string,
> or to take "spaces" ("blanks") away (a typical "wrap" function)??


Not in C. You either have to write your own or get a non-standard
library that someone else wrote.

> About reading data from a text file, I think this is called "parsing".
> Is there any "parsing" library???


Reading the text and parsing it are different tasks. Reading is getting
it in to memory, parsing is breaking it apart in to useful chunks. Some
functions, e.g. fscanf, do both tasks. Genreally in my opinion the best
way is often to read a line at a time in to memory and then parse the
line entirely in memory.

> Sorry if my questions are too naive, but I am a beginner.


The first thing you need to do is decide which language you are trying
to learn. If it is C++ then the best answers are likely to be very
different and could include classes or templates or something else which
C does not have.
--
Flash Gordon
 
Reply With Quote
 
Ersek, Laszlo
Guest
Posts: n/a
 
      01-26-2010
In article <(E-Mail Removed)>, Vicent <(E-Mail Removed)> writes:
> I posted this also in comp.lang.c++, so sorry for multiposting.
>
> I would like to ask you about standard or usual ways to manage with
> files or strings, specially when getting input data and writing output
> from an algorithm.
>
> I mean: which structures, data types or classes? Which standard ways
> to read/write from/on files?


For C++, I guess you'd look first at std::string and std:iostream.
Google them, or obtain a draft or an actual edition of the ISO C++
standard (ISO/IEC 14882). A draft might be available at the C++
Standards Committee's site:

http://www.open-std.org/jtc1/sc22/wg21/

Chapter 21, Strings library
Chapter 27, Input/output library

The Qt or Boost libraries may prove helpful as well.

http://doc.trolltech.com/4.6-snapshot/qstring.html
http://www.boost.org/doc/libs/1_41_0...ies.htm#String


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'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 or functions to get substrings from a string,
> or to take "spaces" ("blanks") away (a typical "wrap" function)??


(That would be a typical "trim" function I guess.) In my opinion, the
"string interface" provided by standard C (or by versions of the Single
Unix Specification) are much lower-level than you'd need; definitiely
not for a beginner with higher abstraction needs. I suggest you switch
to another language supporting high-level string manipulation (Perl,
Python, Ruby etc) or grab a strings library. A discussion on them
occurred on Reddit some time ago; several libraries were mentioned:

http://www.reddit.com/r/programming/...or_a_c_project

When posting to that topic, I stumbled upon the following comparison
page:

http://www.and.org/vstr/comparison


> About reading data from a text file, I think this is called "parsing".
> Is there any "parsing" library???


Especially in relation to parsing: don't start writing parsers in C. If
you must, stick to whole-line input (with bounded length) and regular
expressions. One such regex library is PCRE:

http://www.pcre.org/

But the Single Unix Specification defines a regex facility too.

http://www.opengroup.org/onlinepubs/...h/regex.h.html

If you insist on consuming lines of arbitrary length, consider the
getline() GNU libc extension.

http://www.gnu.org/s/libc/manual/htm...tml#Line-Input

Localized low-level text processing (put very crudely: anything
non-ASCII) requires even more caution, so don't start with that either.
Some C and C++ libraries should support it transparently, though.

HTH,
lacos
 
Reply With Quote
 
Vicent
Guest
Posts: n/a
 
      01-26-2010

> Sounds more like you have a comp.sci problem than a C problem, as in
> learn how to manipulate data first then pick a language to express it.
>
> Also, pick a single language and go with it. *There is no C/C++ or
> whatever. *If you want to learn how to manipulate strings in C++
> that's fine but at what I'm guessing is your level I'd stick to one or
> another, specially since they're not related.
>
> Tom


Tom,

Thank you for your answer.

I've chosen C++, because I need to program some algorithms and I think
it is a good choice for that purpose.

So, my problem is about how to read files in C++, I think.
 
Reply With Quote
 
Vicent
Guest
Posts: n/a
 
      01-26-2010
On 26 ene, 14:07, Flash Gordon <(E-Mail Removed)> wrote:

> If you are using C++ then I believe C++ provides a lot more facilities
> string facilities than C.


Yes, I realize of that...


> A number of people have string libraries which they have written
> themselves, which use more complex structures. However, these are not
> standard.


OK. I didn't know that, although I was suspecting it.


> Reading the text and parsing it are different tasks. Reading is getting
> it in to memory, parsing is breaking it apart in to useful chunks. Some
> functions, e.g. fscanf, do both tasks. Genreally in my opinion the best
> way is often to read a line at a time in to memory and then parse the
> line entirely in memory.


Yes, that was my idea, in fact --First, I read a line. Then, I try to
get the information from that line into some variables in my
algorithm.

> The first thing you need to do is decide which language you are trying
> to learn. If it is C++ then the best answers are likely to be very
> different and could include classes or templates or something else which
> C does not have.


I guess I'll stay with C++.

Thank you!
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-26-2010
On 1/26/2010 11:14 AM, Vicent wrote:
> [...]
> I've chosen C++, because I need to program some algorithms and I think
> it is a good choice for that purpose.
>
> So, my problem is about how to read files in C++, I think.


Perhaps the kind people on the comp.lang.c++ forum
would be better able to assist you with that language.
Since the I/O features of C++ differ quite a lot from
those of C, and since I/O is what you're interested in ...

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Vicent
Guest
Posts: n/a
 
      01-26-2010
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.


> Google them, or obtain a draft or an actual edition of the ISO C++
> standard (ISO/IEC 14882). A draft might be available at the C++
> Standards Committee's site:
>
> http://www.open-std.org/jtc1/sc22/wg21/
>
> Chapter 21, Strings library
> Chapter 27, Input/output library


That's a great link!! Thanks.


> The Qt or Boost libraries may prove helpful as well.
>
> http://doc.trolltech.com/4.6-snapsho...ies.htm#String
>


Good links, also.


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


OK. Everyone tells me to avoid using C-strings, so...

>
> > 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 or functions to get substrings from a string,
> > or to take "spaces" ("blanks") away (a typical "wrap" function)??

>
> (That would be a typical "trim" function I guess.)


Yes, yes, sorry, I meant "trim", not "wrap". I miss those simple
"trim" functions at Visual Basic and PL/SQL Oracle...

> In my opinion, the
> "string interface" provided by standard C (or by versions of the Single
> Unix Specification) are much lower-level than you'd need; definitiely
> not for a beginner with higher abstraction needs. I suggest you switch
> to another language supporting high-level string manipulation (Perl,
> Python, Ruby etc) or grab a strings library. A discussion on them
> occurred on Reddit some time ago; several libraries were mentioned:
>
> http://www.reddit.com/r/programming/..._string_type_s...
>
> When posting to that topic, I stumbled upon the following comparison
> page:
>
> http://www.and.org/vstr/comparison
>


OK, that's all very interesting. I see that other people had the same
problem before me!


> > About reading data from a text file, I think this is called "parsing".
> > Is there any "parsing" library???

>
> Especially in relation to parsing: don't start writing parsers in C. If
> you must, stick to whole-line input (with bounded length) and regular
> expressions. One such regex library is PCRE:
>
> http://www.pcre.org/
>
> But the Single Unix Specification defines a regex facility too.
>
> http://www.opengroup.org/onlinepubs/...h/regex.h.html
>
> If you insist on consuming lines of arbitrary length, consider the
> getline() GNU libc extension.
>
> http://www.gnu.org/s/libc/manual/htm...tml#Line-Input
>
> Localized low-level text processing (put very crudely: anything
> non-ASCII) requires even more caution, so don't start with that either.
> Some C and C++ libraries should support it transparently, though.



What I exactly need to do is the following:

While there are still new lines:
(1) Get one line from a given text file.
(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.

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

- About #2 : It would be as simple as: detecting the position of "="
and then get two substrings. I don't understand why this step is so
difficult to perform in C!!!! I mean: 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"). Is it easier at C++??

- About #3 : I would only need an equivalent of VB's "trim"
function... Is there anything like that at C++?

- About #4 : I can do this by using a "case" or an "if" statement. 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.


So, do you think that C++ std::string and std:iostream classes are
the right choice for me??

Thank you in advance for your feed-back!!!

--
Vicent
 
Reply With Quote
 
Ersek, Laszlo
Guest
Posts: n/a
 
      01-26-2010
In article <20100126182310.7d14676c@kubuntu>, 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...


Please elaborate.

Thanks,
lacos
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      01-26-2010
Vicent 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.
>
>
> > Google them, or obtain a draft or an actual edition of the ISO C++
> > standard (ISO/IEC 14882). A draft might be available at the C++
> > Standards Committee's site:
> >
> > http://www.open-std.org/jtc1/sc22/wg21/
> >
> > Chapter 21, Strings library
> > Chapter 27, Input/output library

>
> That's a great link!! Thanks.
>
>
> > The Qt or Boost libraries may prove helpful as well.
> >
> > http://doc.trolltech.com/4.6-snapsho...ies.htm#String
> >

>
> Good links, also.
>
>
> > 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.

>
> OK. Everyone tells me to avoid using C-strings, so...
>
> >
> > > 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 or functions to get substrings from a string,
> > > or to take "spaces" ("blanks") away (a typical "wrap" function)??

> >
> > (That would be a typical "trim" function I guess.)

>
> Yes, yes, sorry, I meant "trim", not "wrap". I miss those simple
> "trim" functions at Visual Basic and PL/SQL Oracle...
>
> > In my opinion, the
> > "string interface" provided by standard C (or by versions of the Single
> > Unix Specification) are much lower-level than you'd need; definitiely
> > not for a beginner with higher abstraction needs. I suggest you switch
> > to another language supporting high-level string manipulation (Perl,
> > Python, Ruby etc) or grab a strings library. A discussion on them
> > occurred on Reddit some time ago; several libraries were mentioned:
> >
> > http://www.reddit.com/r/programming/..._string_type_s...
> >
> > When posting to that topic, I stumbled upon the following comparison
> > page:
> >
> > http://www.and.org/vstr/comparison
> >

>
> OK, that's all very interesting. I see that other people had the same
> problem before me!
>
>
> > > About reading data from a text file, I think this is called "parsing".
> > > Is there any "parsing" library???

> >
> > Especially in relation to parsing: don't start writing parsers in C. If
> > you must, stick to whole-line input (with bounded length) and regular
> > expressions. One such regex library is PCRE:
> >
> > http://www.pcre.org/
> >
> > But the Single Unix Specification defines a regex facility too.


> What I exactly need to do is the following:
>
> While there are still new lines:
> (1) Get one line from a given text file.
> (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.
>
> - 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.
>
> - About #2 : It would be as simple as: detecting the position of "="
> and then get two substrings. I don't understand why this step is so
> difficult to perform in C!!!! I mean: 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"). Is it easier at C++??
>
> - About #3 : I would only need an equivalent of VB's "trim"
> function... Is there anything like that at C++?
>
> - About #4 : I can do this by using a "case" or an "if" statement. 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.
>
>
> So, do you think that C++ std::string and std:iostream classes are
> the right choice for me??
>
> Thank you in advance for your feed-back!!!
>
> --
> Vicent

 
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