Subj : Re: Fixed-width data reading from FILE/streams To : borland.public.cpp.borlandcpp From : "José Mauro Marinho" Date : Sun Feb 08 2004 08:07 pm Nice, but not so simple, why int and float must be read also. For instance, here is two typical lines: 0091 This is a test 4.56 1.03 0096 This also.. 1035 Moreover, it shall recognize the implicit decimal point for float columns, eg. 1035 => 1.035 (%4.3f ?) Thank you, "Bob Gonder" escreveu na mensagem news:op4d20l9012biplbc1so1iq945rrvc4ute@4ax.com... > José Mauro T. Marinho wrote: > > >Can somebody tell me in a nutshell what is the best way to read fixed-width > >(column delimited) data from a text file. Incredibly, Fortran is very best > >C/C++ in this tedious task... > > First, open the text file in binary mode, not text. > Define a structure that matches your data. > > typedef struct rec{ > char FirstPart[10]; > char SecondPart[27]; > char crlf[2]; > }MYRECORD; > > MYRECORD record; > > Use your choice of file functions to open the file in binary mode. > Then use the matching read function to > read( file, &record, sizeof(MYRECORD) ) > > If your file has cr/lf delimiting the records, don't forget to include > room in your structure for it. > > .