473,396 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

What's the difference between >> and get()?

I'd like to load a text file to memory. The code is like:

void load_dict()
{
static const string DICT("D:\\README.txt");

ifstream dict(DICT.c_str(), std::ios::in);

if (!dict)
{
cerr << "can't open input file: " << DICT << endl;
}

vector<unsigned charraw;
char c;
while (dict.get(c))
{
raw.push_back(c);
}
cout << raw.size() << endl;
}

It says there is 36388 bytes. That's correct.

But if I change the code to 'dict >c', there is only 29658 bytes.
What's wrong with the >operator?
I'm developing under Windows to read unix format file.

And if I change the code to:
unsigned char c;
while (dict.get(c)) ...
There is compilation errors.

What can I do if I want to load binary data?
Jul 15 '08 #1
3 1434
Sam
Lambda writes:
vector<unsigned charraw;
char c;
while (dict.get(c))
{
raw.push_back(c);
}
cout << raw.size() << endl;
}

It says there is 36388 bytes. That's correct.

But if I change the code to 'dict >c', there is only 29658 bytes.
What's wrong with the >operator?
It skips whitespace.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkh798oACgkQx9p3GYHlUOJMXQCcDUrAzBFgsm CJsuZT50u8ZxHJ
zAwAn3emtcrc/VJTJlA/0bSOdRt2Gigm
=3WZu
-----END PGP SIGNATURE-----

Jul 15 '08 #2
"The "get()" and "getline()" functions treat whitespace characters
exactly like other characters. They are intended for input
operations, where one doesn't make assumptions about the meanings of
the characters read." -- Bjarne Stroustrup _The_C+
+_Programming_Language, 3rd edition, section 21.3.4

On the other hand, ">>" skips whitespace. That's why you see a
smaller size in the vector.

As for unsigned char versus "plain" char, the char data type may be
signed or unsigned. "Plain" char is what you should use.

To read a binary file, open the file stream like this:
ifstream dict(DICT.c_str(), std::ios::in | std::ios::binary);
(Haven't compiled this line, but I think that's right.)

Nicolai Josuttis' _The_C++_Standard_Library_ is another good reference
for this stuff.

Good luck!
Jul 15 '08 #3
In article <8bee4f7e-3d16-4201-9966-a2cdbedf8aa4
@k30g2000hse.googlegroups.com>, st*********@gmail.com says...

[ ... ]
But if I change the code to 'dict >c', there is only 29658 bytes.
What's wrong with the >operator?
Nothing -- but (by design) it skips white space in the input.

If you really want to use it, you can turn that off, but for what you're
doing, it sounds like read() is more suitable.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 15 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

129
by: Torbjørn Pettersen | last post by:
I've started cleaning up my HTML and implementing CSS. So far I've used FrontPage, but am switching over to DreamWeaver. Reading a bit on W3Schools.com and W3.org I see there are a lot of HTML...
61
by: Toby Austin | last post by:
I'm trying to replace <table>s with <div>s as much as possible. However, I can't figure out how to do the following… <table> <tr> <td valign="top" width="100%">some data that will...
4
by: Lost Bits | last post by:
Hello there C++ pros.. Well, I have recently redesigned an already existing piece of code that someone else designed - its just a search algorithm they developed, and has to perfomr millions of...
2
by: Andrew Donaldson | last post by:
I'd welcome some help in understanding what's going on with graphical browsers in the navigation list at: http://www.bounceandtickle.org.uk/index.html (This site is not about what it might...
28
by: dcrespo | last post by:
Hi all, How can I get a raised exception from other thread that is in an imported module? For example: --------------- programA.py ---------------
11
by: drbohner | last post by:
OK - I'm not an access dude anymore... 'beer' with me. One of my clients thought they were 'doing the right thing' and cleansed their access program folders. Now, when they attempt to open...
40
by: fordge | last post by:
we usually use <<n as a cheaper option to multiplication when its factor of 2^n right, even for non 2^n say ix24 ==> ix(16+8) ==> (i<<4)+(i<<3) similarly divison 2^n is >>n
8
by: active | last post by:
I use quickwatch on (astrThisOne <> "") and it reports: False as it should because astrThisOne reports: "" Yet If (astrThisOne <> "") Then executes the Then clause
0
by: academic | last post by:
When I show the Designer I get a lot of flashing. What I need to know is: What events are triggered when I open the Designer. In Paint I exit if in DesignerMode and that didn't fix it. ...
19
by: Conrad Lender | last post by:
| 5.40 Why is my Ajax page not updated properly when using an HTTP GET | request in Internet Explorer? | | Microsoft Internet Explorer caches the results of HTTP GET requests. | To ensure that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.