Solved

fstream operator>>(unsigned long)

Posted on 2004-10-08
10
630 Views
Last Modified: 2013-12-14
I am trying to read some data from a file into unsigned long variables and it does not work.  Can anyone help?

                                                                               
#include <fstream>
#include <iostream>
using namespace std;
                                                                               
int main(){
  ifstream in("test.txt");
  unsigned long aLong = 0;
  char aChar = 0;
  cout << "is_open=" << in.is_open() << endl;
  //in >> aLong;
  in >> aChar;
  //cout << "aLong='" << aLong << "'" << endl;
  cout << "aChar='" << aChar << "' (" << (int)aChar << ")" << endl;
}
~

test.txt contains the following string:
abcdef

my output with the two lines commented out is as follows:
is_open=1
aChar='a' (97)

my output with the two lines uncommented is as follows:
is_open=1
aLong='0'
aChar='' (0)

Why can I not read into an unsigned long with a function that implies that it should do this?

klopex
0
Comment
Question by:klopex
  • 5
  • 5
10 Comments
 
LVL 5

Expert Comment

by:ddunlea
ID: 12259166
You are trying to read an unsigned long, but the data you are feeding it is text, not numeric. C++ is typesafe so it will only accept numeric data for a numeric data type.
0
 
LVL 3

Author Comment

by:klopex
ID: 12259217
How does it know?  When it reads the file, it is just a lot of bits.  there is an unsigned long that represents the bit pattern it is finding in the file.  Are you sure?

0
 
LVL 5

Expert Comment

by:ddunlea
ID: 12259273
The stream operators are by default for text based streams, not binary. When trying to read an unsigned long, it wil look for a string of digits. C++ is a high level language. It doesn't just read "a lot of bits". It's more intelligent than that.
0
 
LVL 3

Author Comment

by:klopex
ID: 12259316
Let me add, that what I am actually trying to do is read the header from a .au audio file.  That file is definitely in its correct format and when I try to read from it, I am getting the same results I am getting when I do the example I have included in this question.
0
 
LVL 3

Author Comment

by:klopex
ID: 12259393
What would one do to read in an unsigned long from a file?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:ddunlea
ID: 12259429
The problem is that the files aren't delimited. What I would do is as follows:

char buf[24]; // 6 ulongs x 4
in.get(buf,24);
unsigned long* pMagic = (unsigned long*)(&buf[0]);
unsigned long* pHdrSize = (unsigned long*)(&buf[1]);

etc...

Stream operators are optimised for text streams, and au files are binary, so you don't get much benefit from them.

Regards,
0
 
LVL 3

Author Comment

by:klopex
ID: 12259868
So, there is no built in way to read in an unsigned long built into C++?
Even though there is an operator>>(unsigned long&) method in ifstream?

Or are you saying that the file would have to be set up in some special way for >> to be able to parse it correctly?

BTW:  I have just written my own class that inherits <fstream> and overloads these operators.  It works...  but I am reading the data in one byte at a time...
0
 
LVL 5

Accepted Solution

by:
ddunlea earned 100 total points
ID: 12259960
What I am saying is that the operator >>(unsigned long&) expects to be given data in the form of a string such as "12345678" and to have it delimited with whitespace at the end.

It can't do a very good job on a binary representation of an unsigned long as the size in bytes, the byte ordering etc. can all vary from one platform to the next and so it cannot simply decide to read 4 bytes and cast them, because on a 64bit architecture, and unsigned long could well be 8 bytes etc.
0
 
LVL 3

Author Comment

by:klopex
ID: 12260037
Thank you ddunlea.  I appreciate your help very much!
0
 
LVL 5

Expert Comment

by:ddunlea
ID: 12260069
No problem. Any time.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now