www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/23/16:35:14

Message-ID: <37485B77.A55D972D@enter.net>
Date: Sun, 23 May 1999 15:48:07 -0400
From: Sean <sproctor AT enter DOT net>
X-Mailer: Mozilla 4.51 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Reading multiple-word data (WAS: String arrays...)
References: <LOzMxDAi+sP3EwcQ AT zaynar DOT demon DOT co DOT uk>
<373EE5D9 DOT EF32E9FA AT enter DOT net> <MQEtGCA+5yP3EwcE AT zaynar DOT demon DOT co DOT uk>
NNTP-Posting-Host: 207.16.153.49
X-Original-NNTP-Posting-Host: 207.16.153.49
X-Trace: 23 May 1999 15:47:37 -0400, 207.16.153.49
Organization: Enter.Net
Lines: 44
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Unigni wrote:
> 
> In article <373EE5D9 DOT EF32E9FA AT enter DOT net>, Sean <sproctor AT enter DOT net>
> writes
> >Depends what you're using to read.  If you're using C++ you could do
> >something like this.  I'm assuming you want to know how to put the names
> >in arrays, I'm not really sure what you want.
> 
> >#include <fstream.h>
> >...
> >
> >ifstream infile("filename");
> >char name[5][10];
> >for(int k = 0; k < 5; k++)
> >  infile >> name[k];
> >
> >This would read 5 names of up to 9 characters (not 10, you need the null
> >to end the string) separated by white space from file filename into your
> >array.
> The program now works (after changing some INTs to FLOATs as well, which
> messed up the reading of the data file) -- thanks for your help!
> 
> However, I now have another problem :-(  I can read in single-word lines
> of data from the file, but when I have a space in the line, it only
> reads the data up to the space. I've tried various methods to fix this,
> but none actually work... I'm sure there's probably something really
> simple to do this, but can anybody help improve my extremely limited
> knowledge of C++? :-)
> --
> Philip Taylor
> philip @ zaynar . demon . co . uk
> http://www.zaynar.demon.co.uk/atr - Programming robots!

I don't really understand your problem, but when you're using c++ input
streams, a space and \n are more or less the same...  Whitespace.  If
you want to read a whole line or something into a string you can use
getline member function like (untested, should work though)

ifstream ifile("blah");
char *str = new char[50];
ifile.getline(str, 49);

If that's not what you were asking, then it shouldn't help at all... :)
Sean

- Raw text -


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