Mail Archives: djgpp/1999/09/07/14:48:29
In article <37d2388a DOT 6478128 AT news DOT inet DOT tele DOT dk>, Lasse Krogh Thygesen
<n DOT o DOT s DOT p DOT a DOT m DOT thygesens AT post DOT tele DOT dk> writes
I would write privately but I got a 'no such user' response. Yes, I did
remove the n.o.s.p.a.m. bit.
[snip - reading integers from a text file]
>int main() {
> ifstream in("36.DAT");
>
> string NumberOfDataset;
>
> getline(in, NumberOfDataset);
> }
>
>See I've tried declaring the NumberOfDataset variable as integer but
>it won't work. The error was something about the getline command which
>did not have a version where the NumberOfDataset variable could be an
>integer.
The getline call reads a whole line of text, stripping the \n and
replacing it with \0. What you want to do is read an integer. Try using
the stream extractor:
int i;
in >> i;
To rewrite your program:
- cut here -------------------------------------------------------------
#include <iostream>
#include <fstream>
int main()
{
using namespace std;
ifstream infile("36.dat");
int i = -1;
if(!infile) {
cerr << "Couldn't open 36.dat for input." << endl;
return -1;
}
infile >> i;
do {
cout << i << ' ';
infile >> i;
} while(infile);
infile.close();
return 0;
}
- cut here -------------------------------------------------------------
>The file I read from (36.DAT) has one number on each line, then there
>is a linebreak. The odd thing is that the numbers has one space
>character before and after them.
By default, stream extractors will ignore whitespace - in this case, the
spaces before/after the integers will be ignored, as will the newline
characters. This behaviour can be changed by setf:
stream.setf(ios_base::skipws); // skip whitespace [default]
stream.unsetf(ios_base::skipws); // don't skip whitespace
HTH, and bye for now,
--
Laurence Withers, lwithers AT lwithers DOT demon DOT co DOT uk
http://www.lwithers.demon.co.uk/
- Raw text -