Again substrings and so on

V

Vicent Giner-Bosch

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!
 
V

Victor Bazarov

Vicent said:
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
 
V

Vicent Giner-Bosch

@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!!!
 
V

Vicent Giner-Bosch

Vicent Giner-Bosch ha scritto:


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.

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.


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

Boost also offers boost::lexical_cast for such conversions.

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

OK, thank you.



Christian, thank you very much!!
 

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,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top