By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,116 Members | 1,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,116 IT Pros & Developers. It's quick & easy.

Write and then read from a file

P: n/a
Hi,

How can I write to a file and then be able to read what I wrote to it later
on. I would like to save some information on to a file continuously and then
be able to retrieve that information later on when I need it. Also, what
headers, etc.... do I need to include in my file. Finally, if I want to find
and retrieve only specific information from the file that I wrote to (much
like a seek in a database program) how do it do it.

Thanks a lot in advance
Roy


Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Thu, 14 Jul 2005 11:52:28 UTC, "Roy Gourgi" <ro***@videotron.ca> wrote:
Hi,

How can I write to a file and then be able to read what I wrote to it later
on. I would like to save some information on to a file continuously and then
be able to retrieve that information later on when I need it. Also, what
headers, etc.... do I need to include in my file. Finally, if I want to find
and retrieve only specific information from the file that I wrote to (much
like a seek in a database program) how do it do it.

Thanks a lot in advance
Roy


Roy,

Look up the file operations for your compiler. The legacy C/C++ libraries
support fopen and C++ adds fstream. The actual include files depend on
which
interfaces you want to use and what version of the compiler/standard you are
at. Looking up "fopen" or "fstream" in your help files should get you
started.
The fopen, fclose, fread, fwrite, fseek, etc interfaces are defined in
the include file "stdio.h".

There are any number of ways to organize the data in a file. Most systems
support random access files and various open modes. Your needs will dictate
whether the file is binary or text format, and whether any special index
information is needed.

David
Jul 23 '05 #2

P: n/a
Roy Gourgi wrote:
Hi,

How can I write to a file and then be able to read what I wrote to it later
on. I would like to save some information on to a file continuously and then
be able to retrieve that information later on when I need it. Also, what
headers, etc.... do I need to include in my file. Finally, if I want to find
and retrieve only specific information from the file that I wrote to (much
like a seek in a database program) how do it do it.

Thanks a lot in advance
Roy

Hi Roy,
Here's a simple exampel showing input and out put to a file:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
// File to read and write to
ofstream outFile;
ifstream inFile;

// A small buffer for the reading
char buffer[256];

// We start by writing to the file
outFile.open("example.txt", ios::out | ios::app );

// Check whether the file is open
if(!outFile.is_open()){
cout << "Error opening file" << endl;
return 1;
}

// Lets write something to the file
outFile << "This is a line\n";
outFile << "This is another line\n";

// Done writing - lets close
outFile.close();

// Open the file for reading
inFile.open("example.txt");

// Check whether the file is open
if(!inFile.is_open()){
cout << "Error opening file" << endl;
return 1;
}

// Lets read that from the file
while(!inFile.eof()){
inFile.getline(buffer, 100);
cout << buffer;
}

// Remember to close the file
inFile.close();

return 0;
}

The part about seeking is a bit complicated, hopefully someone has a
good idea there..

--

// Morten - www.mortenvp.dk
Jul 23 '05 #3

P: n/a
Morten V Pedersen wrote:
Roy Gourgi wrote:
Hi,

How can I write to a file and then be able to read what I wrote to it
later on. I would like to save some information on to a file
continuously and then be able to retrieve that information later on
when I need it. Also, what headers, etc.... do I need to include in my
file. Finally, if I want to find and retrieve only specific
information from the file that I wrote to (much like a seek in a
database program) how do it do it.

Thanks a lot in advance
Roy

Hi Roy,
Here's a simple exampel showing input and out put to a file:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
// File to read and write to
ofstream outFile;
ifstream inFile;

// A small buffer for the reading
char buffer[256];

// We start by writing to the file
outFile.open("example.txt", ios::out | ios::app );

// Check whether the file is open
if(!outFile.is_open()){
cout << "Error opening file" << endl;
return 1;
}

// Lets write something to the file
outFile << "This is a line\n";
outFile << "This is another line\n";

// Done writing - lets close
outFile.close();

// Open the file for reading
inFile.open("example.txt");

// Check whether the file is open
if(!inFile.is_open()){
cout << "Error opening file" << endl;
return 1;
}

// Lets read that from the file
while(!inFile.eof()){
inFile.getline(buffer, 100);
cout << buffer;
}

// Remember to close the file
inFile.close();

return 0;
}

The part about seeking is a bit complicated, hopefully someone has a
good idea there..


Sure. seekg/seekp should do the trick. Personally I'd prefer using
the FILE* API though. Of course it also depends on what one wants
to store/load.
Jul 23 '05 #4

P: n/a
> Personally I'd prefer using
the FILE* API though. Of course it also depends on what one wants
to store/load.


Or if one does it in C or C++.
Jonathan

Jul 23 '05 #5

P: n/a
Jonathan Mcdougall wrote:
Personally I'd prefer using
the FILE* API though. Of course it also depends on what one wants
to store/load.

Or if one does it in C or C++.


What's "not C++" in using the FILE* API? Or did you mean something
else? Just because there is a stream library in C++ does not mean
one has to use it or should use it in every situation where one has
to access files...
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.