Solved

fstream operator>>(unsigned long)

Posted on 2004-10-08
10
627 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

708 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

17 Experts available now in Live!

Get 1:1 Help Now