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
Solved

fstream operator>>(unsigned long)

Posted on 2004-10-08
10
636 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
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.

809 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