Thread View: alt.comp.lang.learn.c-c++
3 messages
3 total messages
Started by EL
Sat, 19 Jan 2002 16:11
newbie in turbo c++ 3.0
Author: EL
Date: Sat, 19 Jan 2002 16:11
Date: Sat, 19 Jan 2002 16:11
25 lines
1628 bytes
1628 bytes
i'm using turbo c++ 3.0 for writing a very simple program, criteria as
below:
open a text file which formated as
first 7 char is ID Number, Next 20 char is Name, Next 3 is marks 1, Next 3
is marks 2.
e.g 1002030ANDYLEUNG 100 95
Now I want to open and read the file by using ifstream & ofstream, then read
the line
which specify the length and put it into a defined structure like
struct
{
char *id, *name
int mark1, mark2
}studrec
however, i didn't know how to read the line with specific length of char and
how to loop it until end of file.
Anyone can help me to solve it.
Rgds.,
Newbie
Re: newbie in turbo c++ 3.0
Author: osmium
Date: Sat, 19 Jan 2002 22:37
Date: Sat, 19 Jan 2002 22:37
49 lines
2436 bytes
2436 bytes
El writes:
> i'm using turbo c++ 3.0 for writing a very simple program, criteria as
> below:
>
> open a text file which formated as
> first 7 char is ID Number, Next 20 char is Name, Next 3 is marks 1, Next 3
> is marks 2.
> e.g 1002030ANDYLEUNG 100 95
>
> Now I want to open and read the file by using ifstream & ofstream, then
read
> the line
> which specify the length and put it into a defined structure like
>
> struct
> {
> char *id, *name
> int mark1, mark2
> }studrec
>
> 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.
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 >> s[i].mark1 >> s[i].mark2;
in.get(); // discard '\n' vulnerable to trailing blanks.
i++;
};
One hopes that that forms a base. After debuggging, of course. No error
checking.
Re: newbie in turbo c++ 3.0
Author: 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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads