Mail Archives: djgpp/1999/05/23/16:35:14
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 -