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
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
> 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)??
Boost has a string algorithm library:
http://www.boost.org/doc/libs/1_41_0/doc/html/string_algo.html
It adds a lot of useful string functions which standard C++ does not
provide.
In particular, have a look at the following for string trimming:
http://www.boost.org/doc/libs/1_41_0/doc/html/string_algo/usage.html#id1701360
> About reading data from a text file, I think this is called "parsing".
No, these are different things. You can parse text from sources other
than files, and you can read text from a file without parsing it.
--
Christian Hackl
ha...@sbox.tugraz.at
Milano 2008/2009 -- L'Italia chiam�, s�!
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
> I've been asking this at comp.lang.c forum too,
What for? You were looking for a solution in C++, weren't you?
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.
> What I exactly need to do is the following:
>
> While there are still new lines:
>
> (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.
> (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().
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?
> (3) Take away the possible "blanks" (like a "trim" function would do)
> from those parts.
Boost's string algorithm library has trim functions.
> (5) Translate the second part (it is still a "string") into a number.
Boost also offers boost::lexical_cast for such conversions.
> - 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!!!!
Because strings are difficult to handle in C. In C++, you have
std::string which does all the work for you. See above.
> - 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.
> So, do you think that C++ std::string and std:iostream classes are
> the right choice for me??
Yes.
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!!
> 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 is very useful to have around on your computer if you will
continue to write software in C++. Also, you can install it with a nice
installer program on Windows (judging from your messages' headers, you
do use Windows, don't you?), and most of its contents, including string
algorithms and lexical cast, can be used with just #including header
files, so if you have never used a C++ library before, it should be an
easy start.
You can also install Boost libraries separately, i.e. installing only
those libraries you actually need, but this is probably a little more
complicated than just installing all of them.
> Christian, thank you very much!!
You're welcome :)
> 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...
If you have any plans to continue to write code in C++ in the future, then
I would suggest to make use of Boost libraries as much as possible. It
might take some effort to learn how to use them, but the benefits are that
the Boost libraries are generally of much higher quality than a C++
beginner would be able to code by himself, and the code will be easier to
grok for another programmer.
Paavo