Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

fstream operator>>(unsigned long)

Posted on 2004-10-08
10
Medium Priority
?
647 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

597 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