TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: DAVID BATE
from: JERRY COFFIN
date: 1997-05-22 11:16:00
subject: Windows Program reading from a file.

On (20 May 97) David Bate wrote to All...
 DB> I have been playing with C++ and I ran into a snag.  After looking
 DB> through the books and going through the examples, I can't find,
 DB> anywhere, an example of a windows file reading a file like CONFIG.RA
 DB> that is connected to a record.
The fact that you're writing for Windows makes little difference in file
I/O.  You can still read and write file just like you can in a program
for DOS, UNIX or whatever.
Assuming config.ra contains a fixed layout, you can still define a
struct and use istrea.read() to read in the data.  You might want to
overload operator >> so you can read/write an entire record at a time:
class RA_config {
    struct {
        char user_name[50]; // I don't know what really goes here.
                            // Just a struct to match the layout of a
                            // config.ra file.
    } file_data;
public:
    istream &read(istream &str) {
        return str.read((char *)&file_data, sizeof(file_data));
    }
    void write(ostream &str) const {
        return str.write((char *)&file_data, sizeof(file_data));
    }
};
istream &operator>>(istream &s, RA_config &item) {
    return item.read(s);
}
ostream &operator<<(ostream &s, RA_config const &item) {
    return item.write(s);
}
In this case, you might want to either add member functions to deal with
the configuration data, or you might want to simply overload operator()
(or somesuch) to return a reference to the entire file_data struct.
Which is preferrable depends on the situation.  If you're working with
existing code and want to do a minimum of changing to it, the latter
will likely work well.  If you want to get the maximum benefit of OO
programming, encapsulating the code that deals with the configuration
into a class of its own will be part of that.
In any case, the fact that you happen to be compiling the code under
Windows really isn't particularly relevant to this sort of operation.
About the only place it might make a difference is if you're writing for
Win 3.x, and decide you want to use _lread/_lwrite instead of normal
iostreams for the actual reading and writing.  However, this is one of
the places that proper OO programming will be of assistance; you can
hide the implementation of reading and writing inside the class, so if
you change from one set of functions to another, the majority of your
code won't be affected.
    Later,
    Jerry.
... The Universe is a figment of its own imagination.
--- PPoint 1.90
---------------
* Origin: Point Pointedly Pointless (1:128/166.5)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.