Write and then read from a file

R

Roy Gourgi

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
 
D

David

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
 
P

Paul Groke

Morten said:
Roy said:
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::eek:ut | 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.
 
M

Morten V Pedersen

Roy said:
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::eek:ut | 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..
 
J

Jonathan Mcdougall

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
 
P

Paul Groke

Jonathan said:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top