?
Solved

fstream operator>>(unsigned long)

Posted on 2004-10-08
10
Medium Priority
?
646 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

762 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