Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

write function

25 views
Skip to first unread message

JasBascom

unread,
Mar 11, 2004, 4:02:06 AM3/11/04
to
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

unread,
Mar 11, 2004, 4:56:02 AM3/11/04
to

"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

unread,
Mar 11, 2004, 5:11:10 AM3/11/04
to

"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

unread,
Mar 11, 2004, 6:29:31 AM3/11/04
to
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

unread,
Mar 11, 2004, 11:25:53 AM3/11/04
to
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

unread,
Mar 11, 2004, 12:04:14 PM3/11/04
to

"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

unread,
Mar 11, 2004, 12:42:08 PM3/11/04
to
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

unread,
Mar 11, 2004, 2:28:56 PM3/11/04
to
// 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 new messages