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

Streaming file IO and binary files

Hi,

Kindly excuse my novice question. In all the literature on ifstream
that I have seen, nowhere have I read what happens if you try to read
a binary file using the ">>" operator. I ran into the two problems
while trying to read a binary file.

1). All whitespace characters were skipped
2). Certain binary files gave a core dump

The problems went away when I used the read() member function on the
input file stream instead. Is this the right way to go about?

I was able to recreate my problem using simple sample source as below:

Thanks,
Saleem


#include <iostream>
#include <fstream>

using namespace std;
main(int argc, char* argv[])
{
if(argc < 2)
{
cerr << "usage: " << argv[0] << " <input-file>\n";
return 1;
}

ifstream ifs(argv[1], ios::in|ios::binary);

char ch;
size_t bytesRead = 0;
while(ifs)
{
ifs >ch;
//ifs.read(&ch, 1);
bytesRead ++;
}

cout << "Successfully read " << bytesRead << " bytes\n";
return 0;
}

Jul 25 '07 #1
3 3804
On 25 ec, 08:51, masood.iq...@lycos.com wrote:
Hi,

Kindly excuse my novice question. In all the literature on ifstream
that I have seen, nowhere have I read what happens if you try to read
a binary file using the ">>" operator. I ran into the two problems
while trying to read a binary file.

1). All whitespace characters were skipped
2). Certain binary files gave a core dump

The problems went away when I used the read() member function on the
input file stream instead. Is this the right way to go about?

I was able to recreate my problem using simple sample source as below:

Thanks,
Saleem

#include <iostream>
#include <fstream>

using namespace std;

main(int argc, char* argv[])
{
if(argc < 2)
{
cerr << "usage: " << argv[0] << " <input-file>\n";
return 1;
}

ifstream ifs(argv[1], ios::in|ios::binary);

char ch;
size_t bytesRead = 0;
while(ifs)
{
ifs >ch;
//ifs.read(&ch, 1);
bytesRead ++;
}

cout << "Successfully read " << bytesRead << " bytes\n";
return 0;

}
Hi.

read and write methods are methods you need, so you're right with your
question.

Data are read from/written to binary files as they are. There is no
change. In text files may be done some conversions. It depends on
paltform. For example on DOS/Windows platform it translates line
nedings to CRLF characters (and vice versa). As far as I know there
may be made more translation on some platforms, although I am not able
to give any example.

operator >is used for reading value in text form, however read
method is for reading of value in binary form (same applies to
operator>and write method). Usualy is better to use text form,
because it is more portable among different platforms.

Jul 25 '07 #2
On Jul 25, 8:51 am, masood.iq...@lycos.com wrote:
Kindly excuse my novice question. In all the literature on ifstream
that I have seen, nowhere have I read what happens if you try to read
a binary file using the ">>" operator. I ran into the two problems
while trying to read a binary file.
Attention. The ">>" operator means "parse the next characters
in the file into the target type, interpreting them as text".
More generally, the abstraction of istream is that of a
transparent stream of characters (not raw bytes). All binary
does is control the interface with the OS.
1). All whitespace characters were skipped
Did you reset the skipws flag? If not, that's what you asked it
to do.
2). Certain binary files gave a core dump
Then there's a bug in your library. Good code never core dumps,
regardless of input. (Of course, the bug may simply be that you
forgot to replace the new handler, and aren't catching
bad_alloc. If you're reading into a string, for example, and
the input data contains a couple of GB without any white space,
something is going to give.)
The problems went away when I used the read() member function on the
input file stream instead. Is this the right way to go about?
It depends what you want to do. Read is good when you know you
have a block bytes of fixed size, with some special, possibly
non-text, format.
I was able to recreate my problem using simple sample source as below:
#include <iostream>
#include <fstream>
using namespace std;
main(int argc, char* argv[])
Just a nit, but "implicit int" was removed from C++ a long, long
time ago. This shouldn't compile without a return type for
main.
{
if(argc < 2)
{
cerr << "usage: " << argv[0] << " <input-file>\n";
return 1;
}
ifstream ifs(argv[1], ios::in|ios::binary);
char ch;
size_t bytesRead = 0;
while(ifs)
{
ifs >ch;
//ifs.read(&ch, 1);
bytesRead ++;
}
If the above loop ever core dumps, you should file a bug report.

If you want to just read characters, I'd use get:

while ( ifs.get( ch ) ) {
++ bytesRead ;
}

(Note too that your loop also counts one too many. For an empty
file, for example, it will count 1.)

Read is really for buffers which you will later unformat
yourself.
cout << "Successfully read " << bytesRead << " bytes\n";
return 0;

}
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 25 '07 #3
On Jul 25, 9:06 am, Ondra Holub <ondra.ho...@post.czwrote:
On 25 ec, 08:51, masood.iq...@lycos.com wrote:
Data are read from/written to binary files as they are. There
is no change. In text files may be done some conversions.
It's a bit more subtle than that. Especially with the standard
streams, which do code translation using the codecvt facet of
the imbued locale regardless of whether the file is binary or
text.

Text mode only guarantees textual integrity: you're only
guaranteed to read what you've written if what you've written
consisted only in printable characters, and even then, there are
exceptions. (You're not guaranteed to be able to read trailing
white space, for example. And it's not specified what happens
if the last character written wasn't a '\n'.) On the other
hand, you're guaranteed that a '\n' will result in whatever the
system normally uses as a line separator (e.g. the two character
sequence 0x0D, 0x0A under Windows, or a new record on systems
with record oriented files). And that there are no extra
characters at the end. Also, you can only seek in a limited
number of cases. In binary mode, you'll also get the bytes you
wrote. All of them, not just printable characters. You can
legally write anything, and will reread exactly what you have
written; '\n' will result in one byte being written, with
whatever the encoding of '\n' is on your system. And you can
seek anyway. But you might read extra 0's that you didn't write
at the end of the file.

Also, on some systems, files written in text mode cannot be read
in binary, and vice versa.
It depends on paltform. For example on DOS/Windows platform it
translates line nedings to CRLF characters (and vice versa).
As far as I know there may be made more translation on some
platforms, although I am not able to give any example.
Even on DOS/Windows, a 0x1A in a text input stream is treated as
EOF, and you won't see anything else after it.
operator >is used for reading value in text form, however read
method is for reading of value in binary form (same applies to
operator>and write method).
Operator >formats, as text. Regardless of file mode. Read
extracts char's from the stream, regardless of file mode. I
regularly use >on files opened in binary mode, and there are
cases where it is reasonable to use read on files opened in text
mode.
Usualy is better to use text form,
because it is more portable among different platforms.
It depends what you mean by "portable". If you're writing files
to be read on the same system, or reading files that were
written as text on the same system, text mode gives you a larger
degree of source code portability; a new line will always be the
single character '\n', regardless of how it is represented on
the system. If you're writing files that will be read by many
different systems, you should define a "portable" format for
them, which most likely will require that they be accessed in
binary mode.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 25 '07 #4

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

Similar topics

3
by: James Whitehead | last post by:
I have the following code which works fine downloading smaller files, the trouble is most of the files to be downloaded are large. With large files it just returns a HTTP 500 error. Any ideas...
3
by: A.M-SG | last post by:
Hi, I have a ASP.NET aspx file that needs to pass large images from a network storage to client browser. The requirement is that users cannot have access to the network share. The aspx file...
6
by: | last post by:
Hi all, is there a better way to stream binary data stored in a table in sql 2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We noticed that in certain heavy load scenarios,...
5
by: Manuel Alves | last post by:
Hi, Is it possible to store media files (like windows .wmv) on SQL Server 2005 and stream it back to the client via media server (not just download it)? Regards, Manuel Alves
7
by: Hugh Janus | last post by:
Hi all. Is it possible to take a binary file and out put it as text so that I can store it in a text field of a database? And then later, take the text and 're-stream' it as the original binary...
8
by: poorna | last post by:
hi all i want to upload the video files to the server.. then i encode all the video files into flv files ... and then i am go to streaming ... in the mean while i create the thumbnail image...
5
by: Jim Bancroft | last post by:
Hi everyone, We've have files we'd like to store in a SQL Server blob or text column and make available online for our clients. Instead of linking to a document sitting on a file server, we...
0
by: Anders Borum | last post by:
Hi Peter, Well, as far as I'm concerned there's actually a big difference between the two strategies. First of all, the SQL server may be busy / bandwidth limited while serving requests from...
3
by: Brad | last post by:
I have an aspx page that is sending pdf files to client browsers: it uses a filestream to read the pdf file and response.binarywrite to send content to the browser. This has worked great for years...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.