[Dev-C++] Re: how to do file and streams
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Graeme F. <gr...@dr...> - 2002-05-13 16:44:57
|
Reading and writing to a file is an important and common task. So I will try
to explain the steps required. (As an aside I would do all of this in a
wrapper class - other here may disagree with that because it will have over
heads but it creates a much easer interface)
To open a file:
===============
I will just look at creating a text file. This makes it easier to test that
you have written to the file correctly because you can open it up using
notepad and check that the format is what you desired.
First you need to have a variable to the file stream. I will create a file
which can be used to read the file as well as write to it. It can be
declared as follows:
fstream iofile;
Next the iofile variable needs to be associated with a file this can be done
by using the open method of the fstream class as follows:
iofile.open("MyFileName.txt");
Obviously you can use any name in the quotes, and any extension you like.
Now did the file open work correctly? If it did then iofile will be a
non-zero value so teh following test can check this:
if (!(iofile))
cerr << "File " << "MyFileName.txt" << " couldn't be opened, sorry...\n";
Writing to the file:
====================
First you need to write to the file the following code will write out a line
to the file, in this case it will write the contents of the variable called
line to the file followed by a new line. (The variable line was declared as
a string object, but it should manage any data type that has to output
operator << defined)
iofile << line << "\n";
Reading from the file
=====================
There are many ways to do this, my preferred approach is to read the data in
a line at a time and then extract the information from that line however you
may want to adopt a different approach maybe by using the input operator >>.
However to read a whole line in:
char data [1024];
// read in the next line and store it in details of cObject
iofile.getline(data,1024);
string result (data);
return result;
This code creates a temporary variable called data that is a character array
and will have more space than I need to read in a single line. The data is
then read into this variable using the getline method of the fstream class.
I then copy it to string object called result. This code is part of a
function and I return result where it can then be parsed and the data
extracted. For your application you don't need data to be anywhere near as
large as 1024 chars, and as I said you may want to experiment with the input
operator <<
Position in the file
====================
The file has two "positions" in the file one is related to the writing and
the other is related to the reading. These can be set using the following
functions
iofile.seekg(0) // sets the read (or get) position to the start of the file
iofile.seekp(0) // set the write (or put) position to the start of the file
Buffers
=======
If you are working in mixed mode i.e. read write and then read some more you
will be well advised to flush the buffers to ensure that your data is
actually written to the file rather than sitting on the buffer:
iofile.flush();
Clear the File
==============
There are several ways to empty a file one simple approach is to close the
file and then open it but truncate the file to length zero. This is done as
follows:
// Close the file and then reopen it but truncate to zero
iofile.close();
iofile.open("MyFileName.txt",
ios_base::in|ios_base::out|ios_base::trunc);
This is slightly different from the previous open method, because now it has
a second parameter. This second parameter using flags to tell the program
how it should treat the file. The flags are in the namespace called ios_base
and the three flags used are in (for input - read), out (yeap output -
write) and trunc (truncate to zero)
hope this helps.
Graeme.
>Message: 2
>From: "chromatonus 712" <chr...@ho...>
>To: dev...@li...
>Date: Mon, 13 May 2002 01:16:58 +0000
>Subject: [Dev-C++] how to do file and streams
>
>hi, here is my program that is supposed to do the following:
>
>Enter a series of records from the keyboard, each record consisting of a
>two-word name and an integer,
>example MEG RAM 23, as one entry
>The program will keep a count of the number of entries. After the last one
>is entered, enter a "sentinel" code, some string that will indicate that
the
>data has been completely entered.
>Then the data will be saved to a file in the format of
>#data name # name #.......
>Then the data will be read from the file and displayed on the screen, one
>entry per line....
|