Article View: alt.comp.lang.learn.c-c++
Article #300Re: newbie in turbo c++ 3.0
From: Rich Churcher
Date: Sun, 20 Jan 2002 00:57
Date: Sun, 20 Jan 2002 00:57
46 lines
2572 bytes
2572 bytes
"osmium" <r124c4u2@mediaone.net> writes:
> > however, i didn't know how to read the line with specific length of char
> > and how to loop it until end of file.
>
> Here is a rough cut at it.
>
> struct S
> {
> char id[8]; // pointers are pointless! You *know* the size.
Weeeeellll... ok, but it's not exactly very flexible. Avoid magic
numbers where possible.
> char name[21];
> int mark1;
> int mark2;
> };
> S s[100]; // space for 100 records
> // Assume a text file named in exists and is open.
>
> int i = 0;
> while(in) // cast will return false at EOF
> {
> in.get(s[i].id, 7);
> in.get(s[i].name, 20);
In fact, these will return one character short each
time. istream::get() grabs n-1 characters, so you need:
in.get( s[ i ].id, 8 );
in.get( s[ i ].id, 21 );
> in >> s[i].mark1 >> s[i].mark2;
> in.get(); // discard '\n' vulnerable to trailing blanks.
> i++;
> };
I'd be tempted to get the whole line as a string and process it from
there, probably safer.
--
Cheers, // First time posting? READ THIS:
Rich. // http://snurse-l.org/acllc-c++/faq.html
Technical Details
Message-ID:
<m3it9x7sbe.fsf@localhost.localdomain>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!nntp-relay.ihug.net!lust.ihug.co.nz!ihug.co.nz!not-for-mailReferences:
<a2c5ds$9nj1@imsp212.netvigator.com> <rym28.24537$yC.3013265@typhoon.mn.mediaone.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f8d38,28dabf9af7da2c85 X-Google-Attributes: gidf8d38,public X-Google-Thread: 114592,28dabf9af7da2c85 X-Google-Attributes: gid114592,public X-Google-Thread: 109fba,28dabf9af7da2c85 X-Google-Attributes: gid109fba,public X-Google-ArrivalTime: 2002-01-19 16:35:38 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!nntp-relay.ihug.net!lust.ihug.co.nz!ihug.co.nz!not-for-mail From: Rich Churcher <churcher@ihug.com.au> Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.language,comp.lang.c++ Subject: Re: newbie in turbo c++ 3.0 Date: 20 Jan 2002 11:57:09 +1100 Organization: ihug (Sydney) Lines: 46 Sender: rich@localhost.localdomain Message-ID: <m3it9x7sbe.fsf@localhost.localdomain> References: <a2c5ds$9nj1@imsp212.netvigator.com> <rym28.24537$yC.3013265@typhoon.mn.mediaone.net> NNTP-Posting-Host: bugstomper.ihug.com.au Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: lust.ihug.co.nz 1011486937 12030 203.109.250.24 (20 Jan 2002 00:35:37 GMT) X-Complaints-To: abuse@ihug.co.nz NNTP-Posting-Date: Sun, 20 Jan 2002 00:35:37 +0000 (UTC) User-Agent: Gnus/5.0807 (Gnus v5.8.7) XEmacs/21.1 (Cuyahoga Valley) Cache-Post-Path: bugstomper.ihug.com.au!unknown@p316-tnt1.syd.ihug.com.au X-Cache: nntpcache 2.3.3 (see http://www.nntpcache.org/)