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

non-member extractors

#include <fstream>
#pragma hdrstop
#include <condefs.h>
#pragma argsused
int main(int argc, char* argv[])
{
std::fstream I;
I.open("C:\\File.ext");
if (I.is_open()) {
unsigned short a;
I >> a;
return 0;
}
else
return 1;
}
Is there anything wrong with this? Why does the reading function fail?

Fraser.
Jul 22 '05 #1
7 1290
Fraser Ross wrote:
#include <fstream>
#pragma hdrstop
#include <condefs.h>
#pragma argsused
int main(int argc, char* argv[])
{
std::fstream I;
I.open("C:\\File.ext");
if (I.is_open()) { if (I) {
unsigned short a;
I >> a;
return 0;
}
else
return 1;
}
Is there anything wrong with this? Why does the reading function fail?


How do you know it failed? Are you using a batch file to check
ERRORLEVEL (I'm assuming you're using Windows based on the filename)?
What are the contents of C:\File.ext?
You don't need condefs.h, and it probably shouldn't have angle brackets,
since it's not a standard header.

Jul 22 '05 #2

"red floyd" >
How do you know it failed? If this instruction is added after the read operation test is set to true.
bool test=I.fail();

What are the contents of C:\File.ext? Its unimportant. The file only has to have at least sizeof(unsigned short)
characters.
You don't need condefs.h, and it probably shouldn't have angle brackets,
since it's not a standard header.

Its put there by default with BCB4.
Fraser.
Jul 22 '05 #3
On Sat, 3 Jul 2004 15:24:31 +0100, Fraser Ross
<fraserATmembers.v21.co.unitedkingdom> wrote:

"red floyd" >
How do you know it failed?

If this instruction is added after the read operation test is set to
true.
bool test=I.fail();

What are the contents of C:\File.ext?

Its unimportant. The file only has to have at least sizeof(unsigned
short)
characters.


No that is incorrect, is has to be text which can be converted to an
integer, optionally preceded by whitespace.

You seem to think you are doing a binary read of sizeof(unsigned short)
bytes but that is not the case. You are reading text and that text must be
a number.

This is how you should do a binary read (minus error checking)

std::fstream I("C:\\File.ext", ios_base::binary);
unsigned short a;
I.read(static_cast<char*>(&a), sizeof a);

Use read for binary reads not >>.

john
Jul 22 '05 #4
* Fraser Ross:

* red floyd:

What are the contents of C:\File.ext?


Its unimportant. The file only has to have at least sizeof(unsigned short)
characters.


The code presented attempts to interpret the file contents as a decimal
specification of an 'unsigned short' value.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #5
std::fstream I("C:\\File.ext", ios_base::binary);
unsigned short a;
I.read(static_cast<char*>(&a), sizeof a);


Sorry reinterpret_cast not static_cast, or even a C style cast would do.

john
Jul 22 '05 #6

"John Harrison"
What are the contents of C:\File.ext?

Its unimportant. The file only has to have at least sizeof(unsigned
short)
characters.


No that is incorrect, is has to be text which can be converted to an
integer, optionally preceded by whitespace.

You seem to think you are doing a binary read of sizeof(unsigned short)
bytes but that is not the case. You are reading text and that text must be
a number.

This is how you should do a binary read (minus error checking)

std::fstream I("C:\\File.ext", ios_base::binary);
unsigned short a;
I.read(static_cast<char*>(&a), sizeof a);

Use read for binary reads not >>.


Thanks. Two other questions I've are: Why use ios_base and not ios? Can a
use of read as above not be suitable for some systems?

Fraser.
Jul 22 '05 #7
On Sat, 3 Jul 2004 15:52:14 +0100, Fraser Ross
<fraserATmembers.v21.co.unitedkingdom> wrote:

"John Harrison"
>
>> What are the contents of C:\File.ext?
> Its unimportant. The file only has to have at least sizeof(unsigned
> short)
> characters.


No that is incorrect, is has to be text which can be converted to an
integer, optionally preceded by whitespace.

You seem to think you are doing a binary read of sizeof(unsigned short)
bytes but that is not the case. You are reading text and that text must
be
a number.

This is how you should do a binary read (minus error checking)

std::fstream I("C:\\File.ext", ios_base::binary);
unsigned short a;
I.read(static_cast<char*>(&a), sizeof a);

Use read for binary reads not >>.


Thanks. Two other questions I've are: Why use ios_base and not ios?
Can a
use of read as above not be suitable for some systems?

Fraser.


ios_base is standard but ios will also work since ios is derived from
ios_base. Some old books will say ios since ios_base is a newer addition
to C++.

ios_base defines all the thing that apply to all types of streams. ios
defines things that only apply to character streams, and there is also
wios which defines things that only apply to wide character streams.

read is suitable for all systems but the results on one system will differ
from results on another system. You cannot take the same file and the same
program and run them on two different systems and expect to get the same
results. Similarly you cannot take a file which has been created using
write on one system and then use read on another system and be sure to get
the same data back. It might work, it might not. Basically binary I/O is
not portable.

john
Jul 22 '05 #8

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

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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...
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.