473,407 Members | 2,676 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,407 software developers and data experts.

File Access


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
Jul 22 '05 #1
10 2655
Hi,
"JKop" <NU**@NULL.NULL> wrote in message
news:rI*******************@news.indigo.ie...

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<char>( 10 ) to the file)


Thanks,


Regards, Ron AF Greve -JKop

Jul 22 '05 #2
BTW:
here some link with more info

http://www.cplusplus.com

Regards, Ron AF Greve

"Moonlit" <news moonlit xs4all nl> wrote in message
news:41*********************@news.xs4all.nl...
Hi,
"JKop" <NU**@NULL.NULL> wrote in message
news:rI*******************@news.indigo.ie...

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<char>( 10 ) to the file)


Thanks,


Regards, Ron AF Greve
-JKop


Jul 22 '05 #3
JKop writes:
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.
Jul 22 '05 #4

"JKop" <NU**@NULL.NULL> wrote in message
news:rI*******************@news.indigo.ie...

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

Jul 22 '05 #5
> #include <fstream>
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
Jul 22 '05 #6

"JKop" <NU**@NULL.NULL> wrote in message
news:OG*******************@news.indigo.ie...
#include <fstream>
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?


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
Jul 22 '05 #7
>> 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


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
Jul 22 '05 #8

"JKop" <NU**@NULL.NULL> wrote in message
news:5v*******************@news.indigo.ie...
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


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
Jul 22 '05 #9
In message <rI*******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
How


On 14 September, you knew C++.
(http://groups.google.com/groups?selm...6%40news.indig
o.ie)

No further questions should be required. What went wrong?

--
Richard Herring
Jul 22 '05 #10
Richard Herring posted:
In message <rI*******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
How


On 14 September, you knew C++.
(http://groups.google.com/groups?selm...6%40news.indig
o.ie)

No further questions should be required. What went wrong?


The language, yes. Not the standard library. I haven't found a decent enough
book on it... TC++PL is far too cryptic.

-JKop
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Dino | last post by:
dear all, i've created an application for a customer where the customer can upload ..csv-files into a specified ftp-directory. on the server, a php-script, triggered by a cronjob, reads all the...
2
by: Fran Tirimo | last post by:
I am developing a small website using ASP scripts to format data retrieved from an Access database. It will run on a Windows 2003 server supporting FrontPage extensions 2002 hosted by the company...
3
by: Joe Costa | last post by:
I have written the following code to search for the right file depending on the startup file for "Client Access". The menu database that I made will load the correct config file specific for each...
4
by: Bill | last post by:
I need help closing a CMD window when it is executed from Access. 1) The batch file is called from Access. 2) Access closes, 3) the batch runs a copy of the access database (creating a backup)...
1
by: raydelex | last post by:
I am new to securing a database with logins. My questions is: I want only one database to use a new Workgroup file that I have created, not all the Access databases that I bring up under my...
11
by: sur | last post by:
Hello, My problem is that File.Exists works fine if my file is on my local drive but returns false if its on any other drive. I think that the issue is probably file permissions and so I have...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
13
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it...
16
by: Eran.Yasso | last post by:
Hi, I have a mdb file shared in the LAN. I want to write app that verifies if it's open. If the file is not open, then my app can open the file. if the file is used, then the app won't open it....
5
by: =?Utf-8?B?QWRyaWFuTW9ycmlz?= | last post by:
Hello! I'm trying to copy a file from another computer on the network that I do not have permission with my current logon details to access. If I open the folder using the Windows file manager...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.