when i run the following code
ifstream fin("scenario.chk",ios::in | ios::binary);
ofstream fout("out.chk",ios::out | ios::binary);
while( !fin.eof() )
{
fout.put(fin.get());
}
fin.close();
fout.close();
there seems to be a one byte difference in filesize...
also the copied file seems to be corrupt because of it.
why doesn't this work? and
is there a better way to copy the file?
(note that i dont want to run a "copy file" command... i will be
inserting stuff in the middle) 25 4786
uhh..using a simpler test file.. it does in fact append an extra
character.. i'm just not sure why. i guess.. it needs to try reading
another character before it is flagged as end of file.
it's because i'm reading and writing in binary isn't it?
ÿ would be the eof character...
question is.. how do i check for it?
uhhm... is there a more efficient solution than
while( fin.good() )
{
c = fin.get();
if( c != EOF ) fout.put(c);
}
and having to check every character to see if its the EOF?
"Mark" <mn*******@gmail.com> wrote in message
news:11********************@g47g2000cwa.googlegrou ps.com...
Please don't discard context. I've reproduced it below. when i run the following code
ifstream fin("scenario.chk",ios::in | ios::binary); ofstream fout("out.chk",ios::out | ios::binary);
while( !fin.eof() ) { fout.put(fin.get()); }
fin.close(); fout.close();
there seems to be a one byte difference in filesize... also the copied file seems to be corrupt because of it.
why doesn't this work? and is there a better way to copy the file?
(note that i dont want to run a "copy file" command... i will be inserting stuff in the middle)
it's because i'm reading and writing in binary isn't it?
No. Don't guess. Find out the real problem.
ÿ would be the eof character...
There is no 'EOF character' in C++. 'EOF'
is a *condition* (typically indicated by
a function return value).
question is.. how do i check for it?
See the FAQ: http://www.parashift.com/c++-faq-lite/
The answer is in section 15.
-Mike
"Mark" <mn*******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com... uhhm... is there a more efficient solution than
while( fin.good() ) { c = fin.get(); if( c != EOF ) fout.put(c); }
and having to check every character to see if its the EOF?
There is NO character value defined as 'EOF'.
EOF (end-of-file) is a condition.
Above, all you need is the while(fin.good())
(Attempts to read past end of file will
cause 'good()' to return false.
-Mike
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:D4*****************@newsread1.news.pas.earthl ink.net... "Mark" <mn*******@gmail.com> wrote in message news:11********************@g47g2000cwa.googlegrou ps.com...
Please don't discard context. I've reproduced it below.
when i run the following code
ifstream fin("scenario.chk",ios::in | ios::binary); ofstream fout("out.chk",ios::out | ios::binary);
I also meant to add:
By definition, 'ifstream' objects use 'ios::in' by default,
and 'ofstream' uses 'ios::out'. What you have is not
'wrong', but redundant.
-Mike
i deleted the posts because they were wrong... and i didnt want people
to think i posted 4 times :)
according to the faq you linked me to i should use while(fin >> c)
instead of fin.eof()... but doesn't that skip whitespace characters? or
is it different in binary?
yep...
while( fin >> c )
{
fout << c;
}
removes all whitespace characters...
now i'm going to go out on a limb here and say there is probably one of
those funky flags i can set that lets me read them in too.. but..uh..
how do i set it? (i dont want to ignore ANY characters)
err..took me awhile, but i found it.
fin.unsetf(ios::skipws);
it wont skip anything else, will it?
Mark wrote: when i run the following code
ifstream fin("scenario.chk",ios::in | ios::binary); ofstream fout("out.chk",ios::out | ios::binary);
while( !fin.eof() ) { fout.put(fin.get()); }
fin.close(); fout.close();
there seems to be a one byte difference in filesize... also the copied file seems to be corrupt because of it.
why doesn't this work? and is there a better way to copy the file?
(note that i dont want to run a "copy file" command... i will be inserting stuff in the middle)
Here is one (incomplete) possibility:
// -----------------------
// fcpy.cpp copy a file
// to compile: g++ -o fcpy fcpy.cpp
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char * argv[])
{
if (argc < 3)
{
std::cout << "usage: fcpy src_file dest_file"
<< std::endl;
return 1;
}
// if the src and dest file are the same file...
// Note: on non-unix systems we should compare the names in
// a case insensitive manner.
// we should also expand the file names to their
// fully-qualified form before comparing them.
if (std::string(argv[1]) == std::string(argv[2]))
{
std::cout << "can not copy a file onto itself" << std::endl;
return 1;
}
std::ifstream in;
std::ofstream out;
in.open(argv[1], std::ios_base::in |
std::ios_base::binary);
if (!in)
{
std::cout << "could not open " << argv[1] << std::endl;
return 1;
}
out.open(argv[2], std::ios_base::out |
std::ios_base::binary);
if (!out)
{
std::cout << "could not open " << argv[2] << std::endl;
in.close();
return 1;
}
std::streamsize cnt = 0;
int rc = 0; // assume success
char buf[512];
// while we can read up to 'sizeof(buf)' bytes into 'buf'
while (cnt = in.readsome(buf, sizeof(buf)))
{
// if we can not write the bytes read
if (!out.write(buf, cnt))
{
std::cout << "write error" << std::endl;
rc = 1; // failure
break;
}
}
in.close();
out.close();
// we should probably add code here to delete the dest file
// if 'rc' is not zero...
// zero on success, non-zero on error
return rc;
}
// ---------------------
Regards,
Larry
wow.. you really went all out on that example.. thank you!
wow.. you really went all out on that example.. thank you!
Mark wrote: when i run the following code
ifstream fin("scenario.chk",ios::in | ios::binary); ofstream fout("out.chk",ios::out | ios::binary);
while( !fin.eof() ) { fout.put(fin.get()); }
fin.close(); fout.close();
This is a FAQ (15.5).
eof is not set until *after* an attempt to read past EOF, so you get an
extra read.
The proper idiom is as follows:
char c;
while (fin.get(c))
fout.put(c);
red floyd wrote: The proper idiom is as follows:
char c; while (fin.get(c)) fout.put(c);
It will likely take ages to copy the file char-after-char.
Why not just do
fout << fin.rdbuf();
- J.
On Thu, 19 Jan 2006 03:24:27 GMT, Larry I Smith
<la***********@verizon.net> wrote: Here is one (incomplete) possibility:
// ----------------------- // fcpy.cpp copy a file // to compile: g++ -o fcpy fcpy.cpp
#include <iostream> #include <fstream> #include <string>
int main(int argc, char * argv[]) { if (argc < 3) { std::cout << "usage: fcpy src_file dest_file" << std::endl; return 1; }
// if the src and dest file are the same file... // Note: on non-unix systems we should compare the names in // a case insensitive manner. // we should also expand the file names to their // fully-qualified form before comparing them. if (std::string(argv[1]) == std::string(argv[2])) { std::cout << "can not copy a file onto itself" << std::endl; return 1; }
std::ifstream in; std::ofstream out;
in.open(argv[1], std::ios_base::in | std::ios_base::binary); if (!in) { std::cout << "could not open " << argv[1] << std::endl; return 1; }
out.open(argv[2], std::ios_base::out | std::ios_base::binary);
if (!out) { std::cout << "could not open " << argv[2] << std::endl; in.close(); return 1; }
std::streamsize cnt = 0; int rc = 0; // assume success char buf[512];
// while we can read up to 'sizeof(buf)' bytes into 'buf' while (cnt = in.readsome(buf, sizeof(buf)))
you don't check here for a read error; incomplete read is not detected
{ // if we can not write the bytes read if (!out.write(buf, cnt)) { std::cout << "write error" << std::endl; rc = 1; // failure break; } }
in.close(); out.close();
you don't check the state of 'out' after close(); incomplete write in
close() is not detected // we should probably add code here to delete the dest file // if 'rc' is not zero...
// zero on success, non-zero on error
zero on success and some errors
return rc; }
IMO, one should not use iostreams for anything but tracing out
unimportant data with operator<<. iostreams have a 'deceptive'
interface that effectively hides error handling from the user in
arcane internal states and operator overloadings (operator void*,
operator!). The apparent 'convenience' of iostreams gives a false
sense of security.
Best regards,
Roland Pibinger
On 18 Jan 2006 16:38:58 -0800 in comp.lang.c++, "Mark"
<mn*******@gmail.com> wrote, while( !fin.eof() ) { fout.put(fin.get()); }
eof() doesn't return true until too late.
How about:
for (char ch = fin.get(); fin.good(); ch = fin.get())
fout.put(ch);
is there a better way to copy the file?
fout << fin.rdbuf();
On Thu, 19 Jan 2006 21:22:33 GMT, David Harmon <so****@netcom.com>
wrote: On 18 Jan 2006 16:38:58 -0800 in comp.lang.c++, "Mark"is there a better way to copy the file?
fout << fin.rdbuf();
We already had that in this thread. It's not even half of a solution.
Regards,
Roland Pibinger
Roland Pibinger wrote: On Thu, 19 Jan 2006 03:24:27 GMT, Larry I Smith <la***********@verizon.net> wrote:
Here is one (incomplete) possibility:
Note the 'incomplete' comment above...
[snip]
// while we can read up to 'sizeof(buf)' bytes into 'buf' while (cnt = in.readsome(buf, sizeof(buf)))
you don't check here for a read error; incomplete read is not detected
readsome() is a raw reader, it will read whatever is available on each
'while' iteration, and 'out'write()' will write only what was read.
Although I would restrict this approach to
local streams - not network sockets, etc. { // if we can not write the bytes read if (!out.write(buf, cnt)) { std::cout << "write error" << std::endl; rc = 1; // failure break; } }
in.close(); out.close();
you don't check the state of 'out' after close(); incomplete write in close() is not detected
// we should probably add code here to delete the dest file // if 'rc' is not zero...
// zero on success, non-zero on error
zero on success and some errors
return rc; }
IMO, one should not use iostreams for anything but tracing out unimportant data with operator<<. iostreams have a 'deceptive' interface that effectively hides error handling from the user in arcane internal states and operator overloadings (operator void*, operator!). The apparent 'convenience' of iostreams gives a false sense of security.
Best regards, Roland Pibinger
That sounds like a personal opinion (:
There's a difference between the 'formatted' and 'unformatted' (raw)
stream methods.
If not iostreams, then what would you use - fp = fopen(), fd = open()??
Regards,
Larry
red floyd wrote: char c; while (fin.get(c)) fout.put(c);
now this is the solution i was looking for :)
simple. just what i wanted...
however, i ended up using something quite different for the problem i
am working on
Roland Pibinger wrote: On Thu, 19 Jan 2006 21:22:33 GMT, David Harmon <so****@netcom.com> wrote:
On 18 Jan 2006 16:38:58 -0800 in comp.lang.c++, "Mark"
is there a better way to copy the file?
fout << fin.rdbuf();
We already had that in this thread. It's not even half of a solution.
Hm, and why is that?
- J.
On Fri, 20 Jan 2006 00:42:40 GMT, Larry I Smith
<la***********@verizon.net> wrote: readsome() is a raw reader, it will read whatever is available on each 'while' iteration, and 'out'write()' will write only what was read. Although I would restrict this approach to local streams - not network sockets, etc.
So do I. The point is that read and write operations with iostreams
can fail even on local disks ("disk full") and local networks. The
user may lose valuable data. IMO, one should not use iostreams for anything but tracing out unimportant data with operator<<. iostreams have a 'deceptive' interface that effectively hides error handling from the user in arcane internal states and operator overloadings (operator void*, operator!). The apparent 'convenience' of iostreams gives a false sense of security.
That sounds like a personal opinion (:
It is. But, IMO(!), it's not totally wrong ;-)
If not iostreams, then what would you use - fp = fopen(), fd = open()??
.... encapsulated in a lean C++ wrapper (that doesn't hide my errors).
Best regards,
Roland Pibinger
On Fri, 20 Jan 2006 13:05:23 +0100, Jacek Dziedzic
<jacek@no_spam.tygrys.no_spam.net> wrote: Roland Pibinger wrote: On Thu, 19 Jan 2006 21:22:33 GMT, David Harmon <so****@netcom.com> wrote:
On 18 Jan 2006 16:38:58 -0800 in comp.lang.c++, "Mark"
is there a better way to copy the file?
fout << fin.rdbuf();
We already had that in this thread. It's not even half of a solution.
Hm, and why is that?
Error handling?
Roland Pibinger wrote: On Fri, 20 Jan 2006 13:05:23 +0100, Jacek Dziedzic <jacek@no_spam.tygrys.no_spam.net> wrote:
Roland Pibinger wrote:
On Thu, 19 Jan 2006 21:22:33 GMT, David Harmon <so****@netcom.com> wrote:
On 18 Jan 2006 16:38:58 -0800 in comp.lang.c++, "Mark"
>is there a better way to copy the file?
fout << fin.rdbuf();
We already had that in this thread. It's not even half of a solution.
Hm, and why is that?
Error handling?
What about it? If you want them to, the streams throw upon error.
Just embed it in a try/catch if you want to test for errors.
Otherwise just
if(!fout || !fin) // do something meaningful
Anyway, the OP didn't imply he wanted to check for errors.
- J.
Jacek Dziedzic wrote: Anyway, the OP didn't imply he wanted to check for errors.
- J.
quite right :) my concern is to get something that works first, worry
about error handling later.
Jacek Dziedzic wrote: It will likely take ages to copy the file char-after-char. Why not just do
fout << fin.rdbuf();
- J.
i'm curious though, would rdbuf not have to internally read
char-by-char?
or is it switching between read and write operations that slows it
down?
anyways, we're talking about filesizes less than a mb (about 200kb)..
runs fast enough for me. but like i said, i'm not reading char-by-char
anymore. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Thomas Lotze |
last post by:
Hi,
another question: What's the most efficient way of copying data between
two file-like objects?
f1.write(f2.read()) doesn't seem to me as efficient as it might be, as a
string containing...
|
by: Robert Tarantino |
last post by:
Hello,
I am trying to find a way to create a scheduled task or
service that will copy my local profile folders
under "Documents and settings" to a network drive. This
would allow me to restore...
|
by: Alex Vinokur |
last post by:
Copying files : input to output
===============================
C/C++ Performance Tests
=======================
Using C/C++ Program Perfometer
http://sourceforge.net/projects/cpp-perfometer...
|
by: Martin Ho |
last post by:
I am running into one really big problem.
I wrote a script in vb.net to make a copy of folders and subfolder to
another destination:
- in 'from.txt' I specify which folders to copy
- in...
|
by: somequestion |
last post by:
During copying file , wanna read file Size like this
string CheckFileSize(string fileName)
{
if( fileName == null ) return;
FileInfo fi = new FileInfo(fileName);
return fi.Length.ToString();...
|
by: mike3 |
last post by:
Hi.
I have an interesting problem. The C program presented below takes
around
12 seconds to copy 128 MB of data on my machine. Yet I know the
machine can go
faster since a copying done at a...
|
by: kimiraikkonen |
last post by:
Hi,
I use system.io.file class to copy files but i have a difficulty about
implementing a basic / XP-like progress bar indicator during copying
process.
My code is this with no progress bar,...
|
by: Jim Barlow |
last post by:
Does anyone know why K&R2 uses the term "File Copying" at this point
(1.5.1)?
Also, in the K&R2 answers to exercises maintained by Richard Heathfield,
for Listing KRX113 Mr Heathfield repeatedly...
|
by: writeson |
last post by:
Hi all,
I'm writing some code that monitors a directory for the appearance of
files from a workflow. When those files appear I write a command file
to a device that tells the device how to...
|
by: raylopez99 |
last post by:
Beware newbies: I spent a day before I figured this out: copying a
bitmap (image) file to file is not quite like copying a text file--you
have to do some tricks (see below), like using a...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |