Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

write function

已查看 25 次
跳至第一个未读帖子

JasBascom

未读,
2004年3月11日 04:02:062004/3/11
收件人
if i have a binary file say - outfile, and a text file say - infile.

while(!outfile.write(\*******\).eof)// can i write to infile using the write
function?
{
some code;
};

John Harrison

未读,
2004年3月11日 04:56:022004/3/11
收件人

"JasBascom" <jasb...@aol.com> wrote in message
news:20040311040206...@mb-m23.aol.com...

The simple answer is yes. But lots of things about your question are setting
off alarm bells.

1) Why does the code quote outfile, when the question is about infile?

2) If the file is called infile, why do you want to write to it? If you open
it in input mode only, then you won't be able to write to the file in any
way.

3) Are you aware of the real differences between text and binary I/O in C++?
This confuses a lot of newbies.

It might be better to explain what you are trying to do than just ask a
simple question (to which I've given the simple answer).

john


John Harrison

未读,
2004年3月11日 05:11:102004/3/11
收件人

"John Harrison" <john_an...@hotmail.com> wrote in message
news:c2pd2d$1vrac3$1...@ID-196037.news.uni-berlin.de...

>
> "JasBascom" <jasb...@aol.com> wrote in message
> news:20040311040206...@mb-m23.aol.com...
> > if i have a binary file say - outfile, and a text file say - infile.
> >
> > while(!outfile.write(\*******\).eof)// can i write to infile using the
> write
> > function?
> > {
> > some code;
> > };
> >
>
> The simple answer is yes. But lots of things about your question are
setting
> off alarm bells.
>
> 1) Why does the code quote outfile, when the question is about infile?
>

Also why are you testing for end of file on an outut operation? eof is only
true for input operations.

john


Rolf Magnus

未读,
2004年3月11日 06:29:312004/3/11
收件人
John Harrison wrote:

Further issues:

eof is a function and must be called as outfile.eof(), i.e. with the ().

You should just leave out the eof call at all. Assuming the OP wanted to
read from infile instead of write to outfile (since as already noted,
eof doesn't make sense for writing), it should just be:

while (infile.read(...))

This will make the loop stop on any error condition, while the eof()
test will not, and thus might result in an endless loop if some other
special condition than eof() happens. You can check if eof() was the
cause after the loop.


JasBascom

未读,
2004年3月11日 11:25:532004/3/11
收件人
sorry to be so confusing. Let me explain it in even simpler term.
How do i write the contents of binary file to a text file, I only posted the
write query because this is how i thought it was done.
while(!infile.eof())
{
// write to text file, infile being a binary file.
outfile being a text file.
}

John Harrison

未读,
2004年3月11日 12:04:142004/3/11
收件人

"JasBascom" <jasb...@aol.com> wrote in message
news:20040311112553...@mb-m03.aol.com...

I suspect the question really is, how to I CONVERT a binary file to a text
file.

Newbies find text and binary confusing, I think because they assume C++ is
smarter than it is, they expect the conversion to work by some sort of
magic, the truth is that you have to do the hard work. If you think about
this its the way it must be because there is no one way to convert from
binary to text, it all depends on how you interpret the binary data. And
that interpretation is something that you have to do. C++ can't do it for
you.

If you did this

open an input file in binary mode
open an output file in text mode
read from the input file using read
write to the output file using write

Then the contents of the binary file would be output to the text file, but
they surely would not be converted to text.

What you need to do is this

open an input file in binary mode
open an output file in text mode
read from the input file into some variables, structures, arrays, or
whatever using read
write to the output file the variables, structures, arrays, or whatever
using <<

The conversion from binary to text happens when you use <<.

john


Default User

未读,
2004年3月11日 12:42:082004/3/11
收件人
JasBascom wrote:
>
> sorry to be so confusing. Let me explain it in even simpler term.
> How do i write the contents of binary file to a text file, I only posted the
> write query because this is how i thought it was done.


What in your mind does it mean to "write the contents of binary file to
a text file"? Please explain IN DETAIL what you are trying to do. It
makes little or no sense to me right now.

Brian Rodenborn

David Harmon

未读,
2004年3月11日 14:28:562004/3/11
收件人
// HexDump.cpp - Simple binary file hex dumper
//
// This program is intended to help by:
// 1. Giving you a way to see what bytes are *really* in
// your binary file.
// 2. Providing a simple example to follow of binary file
// input and formatted output.
//
// Discussion on news:comp.lang.c++
//
// Released to the public domain by the author,
// David Harmon, March 2004

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cctype>
using namespace std;

const char usage[] =
"binary file hex dumper\n"
"usage:\n"
" hexdump filename\n";

int main(int argc, char **argv)
{
if(argc != 2) {
cerr << usage;
exit(EXIT_FAILURE);
}

ifstream input(argv[1], ios::in|ios::binary);
if (!input) {
perror(argv[1]);
exit(EXIT_FAILURE);
}

cout << hex << uppercase << setfill('0');
long position = 0;
char buffer[16];

while(input.read(buffer, sizeof buffer).gcount() != 0) {

cout << setw(6) << position << " = ";
position += input.gcount();

for(int ip=0; ip<input.gcount(); ++ip) {
cout << setw(2) << (int)buffer[ip] << ' ';
}
cout << "= |";

for(int it=0; it<input.gcount(); ++it) {
if (isprint(buffer[it]))
cout << buffer[it];
else
cout << '.';
}
cout << "|\n";
}

if (!input.eof()) {
perror(argv[1]);
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}


0 个新帖子