473,408 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Write and then read from a file

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
5 3319
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
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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Nick Evans | last post by:
Hello there, I have been on and off learning to code (with python being the second language I have worked on after a bit of BASIC). What I really want to know is, if you are going to actually...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
10
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus...
5
by: Just Me | last post by:
Using streams how do I write and then read a set of variables? For example, suppose I want to write into a text file: string1,string2,string3 Then read them later. Suppose I want to write...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
2
by: agphoto | last post by:
There is big or problem in open file in read and write mode.. $file = "data.txt"; $fp = fopen($file,"w+"); $line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes echo...
7
by: nass | last post by:
hi all, i am running slackware linux and need to use some function that will will enable me to write and read from a shared mem segment.. i am using open() , to open a file, and then use mmap to...
6
by: ericunfuk | last post by:
Hi ALL, I want to read a binary file(it's pic.tif file, I guess it's binary file?), then write it to a new file), I have several questions about this process: When I use fread() to read a...
5
by: newsaboutgod | last post by:
I think VB.NET drives some people crazy because some simple VB6 things seem so hard. Here is some VB6 code: 'Write CSV File open "c:\test.csv" for output as #1 write#1, "1","2","3","4","5"...
6
by: globalrev | last post by:
i ahve a program that takes certain textsnippets out of one file and inserts them into another. problem is it jsut overwrites the first riow every time. i want to insert every new piece of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.