Home > Archive > VC STL > March 2005 > problems using getline and >> while reading a file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
problems using getline and >> while reading a file
|
|
| Jacob Bensabat 2005-02-14, 9:11 pm |
| Hi
I have an ascii file that comprise lines of text (comments) and numeric data
here follows one example
* title
* version
@2.1000
* number of nodes elements
1000 2000
* number of houses
1000
I know a priori the structure of the file then
I read every comment line using the getline function
This the code that reads this data
....
std::string strLine;
std::string strValue;
long lValue1,lValue2;
std::ifstream inp;
inp.open(filename);
std::getline(inp,strLine); // OK I get the string "* title"
std::getline(inp,strLine); // OK I get the string "* Version"
std::getline(inp,strValue); // OK I get the string "@2.1000"
std::getline(inp,strLine); // OK I get the string "* number of nodes elements"
inp >> lValue1 >> lValue2; // OK I get 1000 and 2000
std::getline(inp,strLine); // NOT OK I get an empty string and from now one
the whole input process is corrupted.
any help would really be appreciated.
jac
| |
| Igor Tandetnik 2005-02-14, 9:11 pm |
| "Jacob Bensabat" <JacobBensabat@discussions.microsoft.com> wrote in
message news:D77E6540-6823-4D4F-BC1D-188C8AF97190@microsoft.com
> I have an ascii file that comprise lines of text (comments) and
> numeric data here follows one example
>
> * title
> * version
> @2.1000
> * number of nodes elements
> 1000 2000
> * number of houses
> 1000
>
> inp.open(filename);
> std::getline(inp,strLine); // OK I get the string "* title"
> std::getline(inp,strLine); // OK I get the string "* Version"
> std::getline(inp,strValue); // OK I get the string "@2.1000"
> std::getline(inp,strLine); // OK I get the string "* number of nodes
> elements" inp >> lValue1 >> lValue2; // OK I get 1000 and 2000
> std::getline(inp,strLine); // NOT OK I get an empty string and from
> now one the whole input process is corrupted.
operator>> for numerical values skips leading whitespace, reads as many
characters as it can recognize as a valid number, and stops at the first
character that is not part of a number. In your case, after reading 2000
into lValue2, the stream is positioned at the line feed character that
immediately follows the number. So the next call to getline reads the
line feed and reports an empty string.
You can simply have one extra call to getline and expect and ignore an
empty string there. Or, you can do this:
inp.ignore(numeric_limits<int>::max(), '\n');
This reads and discards all characters up to and including the nearest
end-of-line character.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
| |
| Jacob Bensabat 2005-02-14, 9:11 pm |
| Hi
Thanks for your message.
I tried to use the numeric_limits class and under VS 2003 it does not work
any way I defined a large int number from the file <limits.h> and it worked.
However when I tried to read a float or double number the string that comes
below becomes corrupted, even though I can continue reading the remainder of
the
file.
Do you have any idea ?
jac
"Igor Tandetnik" wrote:
> "Jacob Bensabat" <JacobBensabat@discussions.microsoft.com> wrote in
> message news:D77E6540-6823-4D4F-BC1D-188C8AF97190@microsoft.com
>
> operator>> for numerical values skips leading whitespace, reads as many
> characters as it can recognize as a valid number, and stops at the first
> character that is not part of a number. In your case, after reading 2000
> into lValue2, the stream is positioned at the line feed character that
> immediately follows the number. So the next call to getline reads the
> line feed and reports an empty string.
>
> You can simply have one extra call to getline and expect and ignore an
> empty string there. Or, you can do this:
>
> inp.ignore(numeric_limits<int>::max(), '\n');
>
> This reads and discards all characters up to and including the nearest
> end-of-line character.
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
>
>
| |
| Wayne A. King 2005-02-14, 9:11 pm |
| On Tue, 1 Feb 2005 13:05:05 -0800, "=?Utf-8?B?SmFjb2IgQmVuc2FiYXQ=?="
<JacobBensabat@discussions.microsoft.com> wrote:
>inp >> lValue1 >> lValue2; // OK I get 1000 and 2000
>std::getline(inp,strLine); // NOT OK I get an empty string and from now one
>the whole input process is corrupted.
Have you tried this?
inp >> lValue1 >> lValue2 >> ws;
--
Wayne A. King
(ba994@torfree.net, wayne.king@ablelink.org,
waking@idirect.com, Wayne_A_King@compuserve.com)
| |
| Tom Widmer 2005-02-14, 9:11 pm |
| Jacob Bensabat wrote:
> Hi
> Thanks for your message.
> I tried to use the numeric_limits class and under VS 2003 it does not work
> any way I defined a large int number from the file <limits.h> and it worked.
std::numeric_limits<int>::max() works on VS 2003.
> However when I tried to read a float or double number the string that comes
> below becomes corrupted, even though I can continue reading the remainder of
> the
> file.
> Do you have any idea ?
Not without seeing the code and the input file and what you mean by
"corrupted", no.
Tom
| |
| Jacob Bensabat 2005-02-14, 9:11 pm |
| Hi
1. the use of std::numeric_limits<long>::Max() does not compile I get
compilation
errors under VS2003 (I use the include file suggested in the help.
2. The data looks like that
* number of nodes , elements
2000 1000
* number of hours
2.000
* number of days
10
the code looks like that
....
std::ifstream inp;
std:string strComment;
std::getline(inp,strComment); // OK I get the right string
inp >> lValue1 >> lValue2; // OK I get the right values
std::getline(inp,strComment); //in order to get to the end of the line
std::getline(inp,strComment); // OK I get the right string
inp >> fltValue;
std::getline(inp,strComment); //in order to get to the end of the line
std::getline(inp,strComment); // OOPs I get a wrong string (some junk in it)
inp >> lValue; // OK I get the right value
thanks
jac
"Tom Widmer" wrote:
> Jacob Bensabat wrote:
>
> std::numeric_limits<int>::max() works on VS 2003.
>
>
> Not without seeing the code and the input file and what you mean by
> "corrupted", no.
>
> Tom
>
| |
| Jacob Bensabat 2005-02-14, 9:11 pm |
| what is the meaning of ws ?
jac
"Wayne A. King" wrote:
> On Tue, 1 Feb 2005 13:05:05 -0800, "=?Utf-8?B?SmFjb2IgQmVuc2FiYXQ=?="
> <JacobBensabat@discussions.microsoft.com> wrote:
>
>
> Have you tried this?
> inp >> lValue1 >> lValue2 >> ws;
>
>
> --
> Wayne A. King
> (ba994@torfree.net, wayne.king@ablelink.org,
> waking@idirect.com, Wayne_A_King@compuserve.com)
>
| |
| Igor Tandetnik 2005-02-14, 9:11 pm |
| "Jacob Bensabat" <JacobBensabat@discussions.microsoft.com> wrote in
message news:D8E05C13-9C1E-4849-B5BA-0CF3C413E3B0@microsoft.com
>
> what is the meaning of ws ?
It reads and discards all whitespace, up to but not including the first
non-whitespace character.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
| |
| Wayne A. King 2005-02-14, 9:11 pm |
| On Thu, 3 Feb 2005 09:43:09 -0800, "=?Utf-8?B?SmFjb2IgQmVuc2FiYXQ=?="
<JacobBensabat@discussions.microsoft.com> wrote:
[color=darkred]
>what is the meaning of ws ?
It is a manipulator which extracts whitespace characters from an
input stream.
--
Wayne A. King
(ba994@torfree.net, wayne.king@ablelink.org,
waking@idirect.com, Wayne_A_King@compuserve.com)
| |
| Craig Kelly 2005-02-14, 9:11 pm |
| "Jacob Bensabat":
> Hi
> 1. the use of std::numeric_limits<long>::Max() does not compile I get
> compilation
> errors under VS2003 (I use the include file suggested in the help.
Sorry to jump in, but: it's max (not Max) and I'm willing to be you're
including windows.h (possibly indirectly)... it defines macros named min and
max.
Add a define for NOMINMAX before including anything and you'll get std stuff
named min and max back.
Craig
| |
| Tom Widmer 2005-02-14, 9:11 pm |
| Jacob Bensabat wrote:
> Hi
> 1. the use of std::numeric_limits<long>::Max() does not compile I get
> compilation
> errors under VS2003 (I use the include file suggested in the help.
>
> 2. The data looks like that
>
> * number of nodes , elements
> 2000 1000
> * number of hours
> 2.000
> * number of days
> 10
>
> the code looks like that
> ...
> std::ifstream inp;
> std:string strComment;
>
> std::getline(inp,strComment); // OK I get the right string
> inp >> lValue1 >> lValue2; // OK I get the right values
> std::getline(inp,strComment); //in order to get to the end of the line
>
> std::getline(inp,strComment); // OK I get the right string
> inp >> fltValue;
> std::getline(inp,strComment); //in order to get to the end of the line
>
> std::getline(inp,strComment); // OOPs I get a wrong string (some junk in it)
> inp >> lValue; // OK I get the right value
>
> thanks
> jac
Hmm, I don't see any problems there, assuming all the reads succeed, but
how are you checking for "junk"? If it's in the debugger, you may be
suffering from the annoying feature of the debugger that it can't
correctly display std::strings longer than 16 characters. Anyway, see
how your code differs from this:
#define NOMINMAX
#include <windows.h>
#include <sstream>
#include <string>
#include <iostream>
#include <limits>
int main()
{
//using stringstream for convenience.
std::istringstream inp(
"* number of nodes , elements\n"
"2000 1000\n"
"* number of hours\n"
"2.000\n"
"* number of days\n"
"10\n"
);
std::string strComment;
int lValue, lValue1, lValue2;
double fltValue;
std::getline(inp,strComment); // OK I get the right string
std::cout << strComment << '\n';
inp >> lValue1 >> lValue2; // OK I get the right values
inp.ignore(std::numeric_limits<int>::max(), '\n'); //in order to
get to the end of the line
std::getline(inp,strComment); // OK I get the right string
std::cout << strComment << '\n';
inp >> fltValue;
inp.ignore(std::numeric_limits<int>::max(), '\n'); //in order to
get to the end of the line
std::getline(inp,strComment); // OOPs I get a wrong string (some
junk in it)
std::cout << strComment << '\n';
inp >> lValue; // OK I get the right value
}
Tom
| |
| Simon Trew 2005-03-07, 4:10 pm |
| "Jacob Bensabat" <JacobBensabat@discussions.microsoft.com> wrote in message
news:91B73727-33B2-46CC-AAEF-FFBF4E31C558@microsoft.com...
> Hi
> Thanks for your message.
> I tried to use the numeric_limits class and under VS 2003 it does not work
> any way I defined a large int number from the file <limits.h> and it
worked.
You need <limits>, not <limits.h>
|
|
|
|
|