File Access

J

JKop

How should I go about file access in a portable C++ program? I've had a
quick look through a reference and seen "fopen". Should I use that? Or has
it been superseeded, sort of like "printf" and "std::cout"?

What I want to do is open an ASCII text file, read some stuff, then erase
certain stuff from the file.


Thanks,


-JKop
 
M

Moonlit

Hi,


JKop said:
How should I go about file access in a portable C++ program? I've had a
quick look through a reference and seen "fopen". Should I use that? Or has
it been superseeded, sort of like "printf" and "std::cout"?
Yes.
#include <fstream>
using namespace std;

ifstream Stream( Filename );
or
for output
ofstream Stream( Filename );

if( Stream.is_open() )
{
Stream << "Data" << endl;
}
What I want to do is open an ASCII text file, read some stuff, then erase
certain stuff from the file.

Be careful on the windows platform. A '\n' is translated to carriage return
linefeed if you open your file without specifying the binary option (happens
even if you just write a static_cast said:

Regards, Ron AF Greve
 
O

osmium

JKop said:
How should I go about file access in a portable C++ program? I've had a
quick look through a reference and seen "fopen". Should I use that? Or has
it been superseeded, sort of like "printf" and "std::cout"?

What I want to do is open an ASCII text file, read some stuff, then erase
certain stuff from the file.

There are two complete *sets* of ways to access I/O in C++. The old way,
inherited from C, and a new way, which is more appropriate for C++. Calling
the new way "iostreams" may be definitive, it may not. A modern C++
compiler places the C headers in <cstdio>. It is more difficult to learn
than the C way, but is, in general, indisputably "better" once you get the
hang of it. The key part of "better" is type safety.

In general, you should not change or erase stuff in a file, you should copy
it to a new file, making the destination different in the changed stuff
area.
 
C

Chris Theis

JKop said:
How should I go about file access in a portable C++ program? I've had a
quick look through a reference and seen "fopen". Should I use that? Or has
it been superseeded, sort of like "printf" and "std::cout"?

What I want to do is open an ASCII text file, read some stuff, then erase
certain stuff from the file.


In principle you can stick with fopen (the old C I/O way) or stick to
streams. It depends very much on your taste and probably the manipulation
you want to do in the file. I personally moved to streams a long time ago as
I find them simply much more comfortable as you can also implement the
respective operators for you classes.

Regards
Chris
 
J

JKop

#include said:
using namespace std;

ifstream Stream( Filename );
or
for output
ofstream Stream( Filename );

if( Stream.is_open() )
{
Stream << "Data" << endl;
}


Well if you use "ifstream" to input from a file and "ofstream" to output to
a file... how do you open a file for reading *and* writing?

Here's an example: You want to find the first occurrence of 'k' in a file.
Once you've found the first 'k', you want to delete everything after until
the next 'm'. Would anyone be kind enough to write a little code snippet
illustrating how... ? or perhaps point me to a webpage?


Thanks,


-JKop
 
J

John Harrison

JKop said:
Well if you use "ifstream" to input from a file and "ofstream" to output
to
a file... how do you open a file for reading *and* writing?
fstream


Here's an example: You want to find the first occurrence of 'k' in a file.
Once you've found the first 'k', you want to delete everything after until
the next 'm'.

That cannot be done. Open one file for reading, open another for writing.
Read everything from the input file, write only what you want to the output
file. Clsoe the files, delete the input file and rename the output.

This is how you do the 'delete something from a file task' whatever file
system you are using.

john
 
J

JKop

Here's an example: You want to find the first occurrence of 'k' in a
That cannot be done. Open one file for reading, open another for
writing. Read everything from the input file, write only what you want
to the output file. Clsoe the files, delete the input file and rename
the output.

This is how you do the 'delete something from a file task' whatever
file system you are using.

john

Thanks!

Hmmm...

So I start off with "doggy.txt". I create a new file called "doggy2.txt".

I open "doggy.txt" with an ifstream, and then I open "doggy2.txt" with an
ofstream... right?

ifstream original("doggy.txt");

ofstream resultant("doggy2.txt");


would I then use:

original.get() until I find 'k', at which point I *stop* copying the
characters to my resultant, until I find 'm' and I start copying them again.

What are they ways of getting a char or a few chars? For instance, I open a
file and I want to check if it has "[Config]\n" at the start? How would I go
about that?


-JKop


-JKop
 
J

John Harrison

JKop said:
Thanks!

Hmmm...

So I start off with "doggy.txt". I create a new file called "doggy2.txt".

I open "doggy.txt" with an ifstream, and then I open "doggy2.txt" with an
ofstream... right?

ifstream original("doggy.txt");

ofstream resultant("doggy2.txt");


would I then use:

original.get() until I find 'k', at which point I *stop* copying the
characters to my resultant, until I find 'm' and I start copying them
again.

Right.

What are they ways of getting a char or a few chars?

One char - get
A few chars - read
For instance, I open a
file and I want to check if it has "[Config]\n" at the start? How would I
go
about that?

getline? But I guess it depends on what you what to do if you don't find
"[Config]\n" on the first line.

john
 

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

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top