473,322 Members | 1,409 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,322 software developers and data experts.

ifstream

ifstream ifs;
ifs.open("whatever.bin", ios::in | ios::binary);

How to set flag above so that if "whatever.bin" file missing, rather than
create a new file named "whatever.bin", but providing a way to detect the
error?

ifs.read((char *)p, 4);

Does each "read()" call above read only in bytes? Is it possible to
directly read in ints? Like ifs.read((int *)p, 1);

ifs.read((char *)p, 16);

How to obtain 4 ints out of p pointing to 16 bytes?

Thanks!
Jul 22 '05 #1
17 3477
"Dart" <da******@dickto.com> wrote...
ifstream ifs;
ifs.open("whatever.bin", ios::in | ios::binary);

How to set flag above so that if "whatever.bin" file missing, rather than
create a new file named "whatever.bin", but providing a way to detect the
error?
Since you use 'ios::in', it shouldn't create. Why would it?

The Standard says that 'open' can set 'failbit', so after an attempt
to open a file, check if it has been in fact open:

if (ifs.is_open())
...

ifs.read((char *)p, 4);

Does each "read()" call above read only in bytes? Is it possible to
directly read in ints? Like ifs.read((int *)p, 1);
No, the low-level I/O works only in chars.

ifs.read((char *)p, 16);

How to obtain 4 ints out of p pointing to 16 bytes?


Often you can simply reinterpret_cast the pointer to treat your 'p'
as an array of ints. It's not portable, of course, use at your own
risk.

Victor
Jul 22 '05 #2
Dart wrote:
ifstream ifs;
ifs.open("whatever.bin", ios::in | ios::binary);
If portability (of the data files) is a concern you should consider
doing formatted input and output instead (with << and >>).
How to set flag above so that if "whatever.bin" file missing, rather than
create a new file named "whatever.bin", but providing a way to detect the
error?
On my system the behaviour is already what you're asking for. I can't
attest to the portability of this, as the standard just says that the
file is opened "as if" by calling fopen, and my documentation for fopen
isn't clear on this point.
ifs.read((char *)p, 4);

Does each "read()" call above read only in bytes?
Yes.
Is it possible to directly read in ints? Like ifs.read((int *)p, 1);
It depends what you mean by directly. Here's an idea:

template <typename T>
std::istream & read_natively (std::istream & stream, T & var)
{
char * mem = reinterpret_cast <char *> (& var);
return stream.read (mem, sizeof var);
}

// ...

int x;
if (read_natively (ifs, x)) /* ... */;
ifs.read((char *)p, 16);

How to obtain 4 ints out of p pointing to 16 bytes?


int a [4];
read_natively (ifs, a); // might not work on broken old compilers

--
Regards,
Buster.
Jul 22 '05 #3
Victor Bazarov <v.********@comAcast.net> wrote in message news:du2fc.32356>
ifs.read((char *)p, 16);

How to obtain 4 ints out of p pointing to 16 bytes?


Often you can simply reinterpret_cast the pointer to treat your 'p'
as an array of ints. It's not portable, of course, use at your own
risk.

Victor

Is this going to work?

int arr[4];
arr = reinterpret_cast<int *>(p);

Thanks!
Jul 22 '05 #4
Dart wrote:

Is this going to work?

int arr[4];
arr = reinterpret_cast<int *>(p);


Of course not. You can't assign to an array.

int *ip = reinterpret_cast<int *>(p);

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #5
Dart wrote:
Is this going to work?

int arr[4];
arr = reinterpret_cast<int *>(p);


No, it's a syntax error. Your compiler would have told you this
if you had taken a minute to try. What was it you wanted to do?

--
Regards,
Buster.
Jul 22 '05 #6
"Buster" <no***@nowhere.com> wrote in message
news:c5**********@newsg3.svr.pol.co.uk...

If portability (of the data files) is a concern you should consider
doing formatted input and output instead (with << and >>).


Are you talking of the big endian and little endian thing?

13 = 0000 1101

may be stored as

0000 1101
1011 0000

Jul 22 '05 #7

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:EM********************@bgtnsc04-news.ops.worldnet.att.net...
"Buster" <no***@nowhere.com> wrote in message
news:c5**********@newsg3.svr.pol.co.uk...

If portability (of the data files) is a concern you should consider
doing formatted input and output instead (with << and >>).


Are you talking of the big endian and little endian thing?

13 = 0000 1101

may be stored as

0000 1101
1011 0000


Big endian, little endian is just one example of the way in which binary I/O
is completely non-portable between different systems.

john
Jul 22 '05 #8
John Harrison wrote:

Big endian, little endian is just one example of the way in which binary I/O
is completely non-portable between different systems.


Depends a lot on how you use it. There are hundreds of binary file
formats in common use that are portable to most common systems.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #9

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:Vy*****************@newsread2.news.pas.earthl ink.net...
John Harrison wrote:

Big endian, little endian is just one example of the way in which binary I/O is completely non-portable between different systems.


Depends a lot on how you use it. There are hundreds of binary file
formats in common use that are portable to most common systems.


That's the 'in the real world' answer, which is fair comment. I was giving
the 'C++ standard' answer which says that binary I/O is non portable.

john
Jul 22 '05 #10
John Harrison wrote:

That's the 'in the real world' answer, which is fair comment. I was giving
the 'C++ standard' answer which says that binary I/O is non portable.


Honestly, I'm not sure I understand why. The only really hard problem I
see is the possibility of CHAR_BIT > 8. Other problems I can think of
are hard, but not impossible (like floating point values -- you can
choose a standard format, but translating to and from it could be quite
tricky, and precision may be lost).

Would you care to explain your take on it?

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #11

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:Vg*****************@newsread1.news.pas.earthl ink.net...
John Harrison wrote:

That's the 'in the real world' answer, which is fair comment. I was giving the 'C++ standard' answer which says that binary I/O is non portable.


Honestly, I'm not sure I understand why. The only really hard problem I
see is the possibility of CHAR_BIT > 8. Other problems I can think of
are hard, but not impossible (like floating point values -- you can
choose a standard format, but translating to and from it could be quite
tricky, and precision may be lost).

Would you care to explain your take on it?


On what? The decision of the C++ standards committee to say that binary I/O
is non-portable? I would imagine that they just didn't want to constrain
implementations of C++. For instance with floating point formats, you would
either have to force every one to use a particular format internally or to
use different formats internally and convert on input or output. The first
is obviously a non-starter with the different floating point hardware out
there, and the second rather defeats the purpose of binary I/O which is
maximum efficiency without regard for anything else. If you are going to do
conversions then you might as well convert to text.

At least that's my take on it.

john
Jul 22 '05 #12
John Harrison wrote:

On what? The decision of the C++ standards committee to say that binary I/O
is non-portable?
Could you indicate where they say that? I'd like to take a look at it.
I would imagine that they just didn't want to constrain
implementations of C++. For instance with floating point formats, you would
either have to force every one to use a particular format internally or to
use different formats internally and convert on input or output. The first
is obviously a non-starter with the different floating point hardware out
there, and the second rather defeats the purpose of binary I/O which is
maximum efficiency without regard for anything else. If you are going to do
conversions then you might as well convert to text.

At least that's my take on it.


Fair enough. Though it's certainly possible that one might want to read
and/or write files in one of the many already-in-use binary formats.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #13
Kevin Goodsell wrote:
John Harrison wrote:

That's the 'in the real world' answer, which is fair comment. I was
giving the 'C++ standard' answer which says that binary I/O is non
portable.


Honestly, I'm not sure I understand why. The only really hard problem
I see is the possibility of CHAR_BIT > 8. Other problems I can think
of are hard, but not impossible (like floating point values -- you can
choose a standard format, but translating to and from it could be
quite tricky, and precision may be lost).


Another problem is that on one system, int might be 16 bit, on another
32 bit, and on the third 24 bit. Then there could be alingment
problems, writing something to disk with another alignment than would
be needed when read in again.

Jul 22 '05 #14
Rolf Magnus wrote:
Another problem is that on one system, int might be 16 bit, on another
32 bit, and on the third 24 bit. Then there could be alingment
problems, writing something to disk with another alignment than would
be needed when read in again.


This shouldn't be a problem if it's done properly, though. The file
format dictates the size of the field and the byte order -- just read in
that many bytes into a byte array, then shift and add those bytes into
your variable as needed (just make sure you choose a type that's large
enough).

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #15

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:%I*****************@newsread2.news.pas.earthl ink.net...
John Harrison wrote:

On what? The decision of the C++ standards committee to say that binary I/O is non-portable?


Could you indicate where they say that? I'd like to take a look at it.


I'm not sure that they do say it in so many words. I think you'd have to
cross reference several sections to work it out. If anyone else knows better
I'd also be interested.

john
Jul 22 '05 #16
Kevin Goodsell <us*********************@neverbox.com> wrote in message
news:ID*****************@newsread2.news.pas.earthl ink.net...
Dart wrote:

Is this going to work?

int arr[4];
arr = reinterpret_cast<int *>(p);


Of course not. You can't assign to an array.

int *ip = reinterpret_cast<int *>(p);

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


The test program is:

char a[4];
a[0] = 0x12;
a[1] = 0x34;
a[2] = 0x56;
a[3] = 0x67;

int* intp = reinterpret_cast<int *>(a);
cout << hex << intp;

The output is: 0012FED4.

Why 0x12, 0x34, 0x56, 0x67 not displayed?

When declaring an array, whatever_array[N], is it always that all elements
in the array are consecutive in memory?
Jul 22 '05 #17
"Dart" <da******@dickto.com> wrote in message
news:uVnfc.25944$K_.670908@bgtnsc05-
The test program is:

char a[4];
a[0] = 0x12;
a[1] = 0x34;
a[2] = 0x56;
a[3] = 0x67;

int* intp = reinterpret_cast<int *>(a);
cout << hex << intp;
Note this is disaster if sizeof(int) is more than 4 bytes. There are some 8
byte machines out there.
The output is: 0012FED4.

Why 0x12, 0x34, 0x56, 0x67 not displayed?
You're printing the address of the pointer. Probably you meant to use *intp
cout << hex << *intp;
I'm using Borland 6, AMD, Windows ME. My result is with *intp is
"67563412". Would this be big endian or little endian?
When declaring an array, whatever_array[N], is it always that all elements
in the array are consecutive in memory?


Yes.
Jul 22 '05 #18

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

Similar topics

2
by: Dave Johnston | last post by:
Hi, I'm currently trying to create a wrapper that uses C functions but behaves like ifstream (from fstream.h) - this is because the platform I'm using (WinCE) doesn't support streams and this is...
7
by: Anton Ishmurzin | last post by:
Greetings All, I think everybodyknows the answer already. But i am quite a newbie in c++. I've got the following line in my code: ifstream ini_file_in("filename.dat", ios::in); But, the...
6
by: Herv? LEBAIL | last post by:
Hi everybody, I'm writing a program which use the <string>, <vector> and <ifstream> classes. Given an array of string, i.e vector<string> file_names, example : file_names = "file1.txt"...
6
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber"...
6
by: csvka | last post by:
Hello, I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate...
4
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream...
10
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
1
by: Xiaozhou.Yin | last post by:
Hi~ In the program,I first used the ifstream variable fin open the file and,open it again after called the fin.close().But the fin is fieled to open the file in the second time.The book I'm...
11
by: adramolek | last post by:
So... I'm trying to get used to using C++ ifstream (or ofstream) instead of stdio (although I'm having second thoughts). Anyways, I want to be able to display a meaningful error message if ifstream...
5
by: DrSchwartz | last post by:
Hi there, I have to read in from file, and then process it. It looks like this. #include <iostream> #include <string> #include <fstream> using namespace std; ... int main() {
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.