Again substrings and so on

Discussion in 'C++' started by Vicent Giner-Bosch, Jan 26, 2010.

  1. Sorry for asking about the same subject again, but I am a bit lost.

    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 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
     
    Vicent Giner-Bosch, Jan 26, 2010
    #1
    1. Advertising

  2. Vicent Giner-Bosch wrote:
    > Sorry for asking about the same subject again, but I am a bit lost.
    >
    > 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.


    There are probably as many answers as there are people you ask.

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


    'istream', 'ostream', 'ifstream', 'ofstream', 'string'...

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


    Why have you been reading C tutorials when your language is C++? Get
    yourself a copy of "The C++ Standard Library: A Tutorial and a
    Reference" by Josuttis.

    > but specially when managing strings, I am a bit
    > lost: Are there methods or functions to get substrings from a string,


    'std::string' has the 'substr' member.

    > or to take "spaces" ("blanks") away (a typical "wrap" function)??


    Not sure what 'a typical "wrap" function' is, but anything can be
    removed from a 'string' by means of 'erase' member function.

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


    I am sure there are quite a few. All depends on what kind of "parsing"
    you need.

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


    It would seem that you need some basic course on C++ before you embark
    on writing or even using a parser. It's hard to advise anything without
    knowing your level. What C++ books have you read so far?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 26, 2010
    #2
    1. Advertising

  3. @Christian and @Victor,

    First of all, I didn't mean "wrap" by "trim" --I mixed them up, sorry.
    The typical "trim" function which is present at Visual Basic, PL/SQL
    Oracle...

    Thank you for your feed-back. I think Boost library could be what I am
    looking for. Thank you also for the book reference. In fact, I've
    never read a whole book about any programming language, but I use to
    constantly look up information in many books, tutorials, etc. --and
    also at the Internet, of course! But I get your advice.

    I've been asking this at comp.lang.c forum too, and I copy here a
    more detailed explanation of what I am looking for, which I've already
    published there:

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


    I hope this helps to understand where I am and where I want to go...

    Thank you for your valuable feed-back!!!

    --
    Vicent
     
    Vicent Giner-Bosch, Jan 26, 2010
    #3
  4. On 26 ene, 18:36, Christian Hackl <> wrote:
    > Vicent Giner-Bosch ha scritto:
    >
    > > I've been asking this at  comp.lang.c  forum too,

    >
    > What for? You were looking for a solution in C++, weren't you?


    Yes, but in fact most of my code is C-compliant, so I thought all my
    program could actually feet into C. But now I realize that it is not
    necessary and that, in fact, it is convenient for me to use some C++
    particular features, like strings.


    > The solution you get in comp.lang.c will work perfectly for C but

    be an
    > ugly hack in C++ because it will involve the use of char* instead of
    > std::string. That's because C and C++ are different programming
    > languages with different purposes and different philosophies.


    Now I get it.


    > > (1) Get one line from a given text file.

    >
    > Have a look at the function std::getline. You pass it a std::string and
    > a std::ifstream, and the next line from the text file will be saved in
    > the std::string.


    So easy... Thank you. I am a bit lost when using classes and so on,
    but I know it is easy. Just... I'm not used to.



    > > (2) In that line, detect a "first" part and a "second part", which are
    > > separated by a "=" symbol.

    >
    > There are a couple of ways to achieve what you want. Have a look at
    > std::string's member functions substr() and find().


    Thanks again.


    > However, you should define these rules with more exceptional cases in
    > mind. For example, what exactly should the program do if more than one
    > "=" appears in the line? What should it do if no "=" can be found?


    Yes, I already thought about that. I'll deal with it later in my code,
    but no problem with that.

    > > (3) Take away the possible "blanks" (like a "trim" function would do)
    > > from those parts.

    >
    > Boost's string algorithm library has trim functions.


    My question is this: Is Boost string library too "heavy" to include? I
    mean, if I only use it "for a while", if I am not going to use it "a
    lot", is it worthy to use it? I DO think so, but I would like to read
    more opinions...


    > > (5) Translate the second part (it is still a "string") into a number.

    >
    > Boost also offers boost::lexical_cast for such conversions.


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

    >
    > No, the casting operators provided by standard C++ are not enough.
    > Again, I recommend boost::lexical_cast.


    OK, thank you.


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

    >
    > Yes.
    >



    Christian, thank you very much!!
     
    Vicent Giner-Bosch, Jan 26, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. amit
    Replies:
    0
    Views:
    412
  2. che
    Replies:
    2
    Views:
    521
  3. abcd

    Importing again and again

    abcd, Jun 8, 2006, in forum: Python
    Replies:
    9
    Views:
    341
    Maric Michaud
    Jun 9, 2006
  4. Ò»Ê×Ê«

    A design problem I met again and again.

    Ò»Ê×Ê«, Apr 1, 2009, in forum: Python
    Replies:
    24
    Views:
    755
    Dennis Lee Bieber
    Apr 4, 2009
  5. Alexander Burger

    jsf 2.0 datatable reload again and again

    Alexander Burger, Sep 23, 2010, in forum: Java
    Replies:
    14
    Views:
    4,424
    Arved Sandstrom
    Sep 26, 2010
Loading...

Share This Page