delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/07/14:48:29

From: Laurence Withers <lwithers AT lwithers DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Newbie Question - Convert string to numerical value
Date: Tue, 7 Sep 1999 03:03:22 +0100
Message-ID: <G92sDNAqJH13EAuG@lwithers.demon.co.uk>
References: <37d32cc7 DOT 4183133 AT news DOT inet DOT tele DOT dk>
<37d39de7 DOT 4581168 AT news DOT enter DOT net> <37d2388a DOT 6478128 AT news DOT inet DOT tele DOT dk>
NNTP-Posting-Host: lwithers.demon.co.uk
X-NNTP-Posting-Host: lwithers.demon.co.uk:194.222.80.1
X-Trace: news.demon.co.uk 936707975 nnrp-03:1484 NO-IDENT lwithers.demon.co.uk:194.222.80.1
X-Complaints-To: abuse AT demon DOT net
MIME-Version: 1.0
X-Newsreader: Turnpike (32) Version 4.01 <WCcCG$mmNL32sYSf46vBeC80CR>
Lines: 73
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019