non-member extractors

F

Fraser Ross

#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.
 
R

red floyd

Fraser said:
#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.
 
F

Fraser Ross

"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.
 
F

Fraser Ross

"John Harrison"
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.
 
J

John Harrison

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


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
 
A

Alf P. Steinbach

* Fraser Ross:
* red floyd:

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.
 
J

John Harrison

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
 
J

John Harrison

"John Harrison"

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
 
R

Robbie Hatley

Fraser Ross said:
#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?

I wouldn't use an fstream to just read text. I tried
your program, and it read from a text file with ASCII
representations of numbers in it, but it also corrupted
the file, adding '3's to the beginning.

Use an ifstream instead.
Why does the reading function fail?

Probably because the file you're trying to open and
read from either doesn't exist, or it doesn't contain
ASCII representations of short integers separated by
white spaces.

Here's an improved version of your program:


// ======= C:\test.txt =======
34 92 61485 2 817


// ======= inserter-test.cpp =======
#include <iostream>
#include <fstream>
using std::ios_base;
using std::cout;
using std::cerr;
using std::endl;
using std::ifstream;
int main()
{
ifstream I ("C:\\test.txt");
if (!I.good() || !I.is_open())
{
cerr << "Error: Couldn't open file!" << endl;
exit(666);
}
unsigned short int a = 0;
while (1)
{
I >> a;
if (I.bad())
{
cerr << "Error: Input bad!" << endl;
I.close();
exit(666);
}
else if (I.eof())
{
cout << "End of file." << endl;
break;
}
else if (I.fail())
{
cerr << "Error: Input fail!" << endl;
I.close();
exit(666);
}
else
{
cout << "Number is: " << a << endl;
}
}
I.close();
return 0;
}


// ======= RESULTS: =======
wd=C:\C\test
%inserter-test
Number is: 34
Number is: 92
Number is: 61485
Number is: 2
Number is: 817
End of file.


--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

template 5
More than one instance. 18
Linux: using "clone3" and "waitid" 0
Crossword 2
Confused about casts 6
[ifstream] Double reading of the last character. 2
Print with command-line arguments 0
linker error 26

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top