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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lambda

    #1

    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:\\READM E.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?
  • Sam

    #2
    Re: What's the difference between &gt;&gt; and get()?

    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)

    iEYEABECAAYFAkh 798oACgkQx9p3GY HlUOJMXQCcDUrAz BFgsmCJsuZT50u8 ZxHJ
    zAwAn3emtcrc/VJTJlA/0bSOdRt2Gigm
    =3WZu
    -----END PGP SIGNATURE-----

    Comment

    • Eric Johnson

      #3
      Re: What's the difference between &gt;&gt; and get()?

      "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_L anguage, 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::binar y);
      (Haven't compiled this line, but I think that's right.)

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

      Good luck!

      Comment

      • Jerry Coffin

        #4
        Re: What's the difference between &gt;&gt; and get()?

        In article <8bee4f7e-3d16-4201-9966-a2cdbedf8aa4
        @k30g2000hse.go oglegroups.com> , stephenhsu9@gma il.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.

        Comment

        Working...