non-member extractors

Discussion in 'C++' started by Fraser Ross, Jul 3, 2004.

  1. Fraser Ross

    Fraser Ross Guest

    #include <fstream>
    #pragma hdrstop
    #include <condefs.h>
    #pragma argsused
    int main(int argc, char* argv[])
    {
    std::fstream I;
    I.open("C:\\File.ext");
    if (I.is_open()) {
    unsigned short a;
    I >> a;
    return 0;
    }
    else
    return 1;
    }


    Is there anything wrong with this? Why does the reading function fail?

    Fraser.
    Fraser Ross, Jul 3, 2004
    #1
    1. Advertising

  2. Fraser Ross

    red floyd Guest

    Fraser Ross wrote:
    > #include <fstream>
    > #pragma hdrstop
    > #include <condefs.h>
    > #pragma argsused
    > int main(int argc, char* argv[])
    > {
    > std::fstream I;
    > I.open("C:\\File.ext");
    > if (I.is_open()) {

    if (I) {

    > unsigned short a;
    > I >> a;
    > return 0;
    > }
    > else
    > return 1;
    > }
    >
    >
    > Is there anything wrong with this? Why does the reading function fail?
    >


    How do you know it failed? Are you using a batch file to check
    ERRORLEVEL (I'm assuming you're using Windows based on the filename)?
    What are the contents of C:\File.ext?
    You don't need condefs.h, and it probably shouldn't have angle brackets,
    since it's not a standard header.
    red floyd, Jul 3, 2004
    #2
    1. Advertising

  3. Fraser Ross

    Fraser Ross Guest

    "red floyd" >
    > How do you know it failed?

    If this instruction is added after the read operation test is set to true.
    bool test=I.fail();


    > What are the contents of C:\File.ext?

    Its unimportant. The file only has to have at least sizeof(unsigned short)
    characters.

    > You don't need condefs.h, and it probably shouldn't have angle brackets,
    > since it's not a standard header.

    Its put there by default with BCB4.


    Fraser.
    Fraser Ross, Jul 3, 2004
    #3
  4. Fraser Ross

    Fraser Ross Guest

    "John Harrison"
    > >
    > >> What are the contents of C:\File.ext?

    > > Its unimportant. The file only has to have at least sizeof(unsigned
    > > short)
    > > characters.

    >
    > No that is incorrect, is has to be text which can be converted to an
    > integer, optionally preceded by whitespace.
    >
    > You seem to think you are doing a binary read of sizeof(unsigned short)
    > bytes but that is not the case. You are reading text and that text must be
    > a number.
    >
    > This is how you should do a binary read (minus error checking)
    >
    > std::fstream I("C:\\File.ext", ios_base::binary);
    > unsigned short a;
    > I.read(static_cast<char*>(&a), sizeof a);
    >
    > Use read for binary reads not >>.


    Thanks. Two other questions I've are: Why use ios_base and not ios? Can a
    use of read as above not be suitable for some systems?

    Fraser.
    Fraser Ross, Jul 3, 2004
    #4
  5. On Sat, 3 Jul 2004 15:24:31 +0100, Fraser Ross
    <fraserATmembers.v21.co.unitedkingdom> wrote:

    >
    > "red floyd" >
    >> How do you know it failed?

    > If this instruction is added after the read operation test is set to
    > true.
    > bool test=I.fail();
    >
    >
    >> What are the contents of C:\File.ext?

    > Its unimportant. The file only has to have at least sizeof(unsigned
    > short)
    > characters.


    No that is incorrect, is has to be text which can be converted to an
    integer, optionally preceded by whitespace.

    You seem to think you are doing a binary read of sizeof(unsigned short)
    bytes but that is not the case. You are reading text and that text must be
    a number.

    This is how you should do a binary read (minus error checking)

    std::fstream I("C:\\File.ext", ios_base::binary);
    unsigned short a;
    I.read(static_cast<char*>(&a), sizeof a);

    Use read for binary reads not >>.

    john
    John Harrison, Jul 3, 2004
    #5
  6. * Fraser Ross:
    >
    > * red floyd:
    > >
    > > What are the contents of C:\File.ext?

    >
    > Its unimportant. The file only has to have at least sizeof(unsigned short)
    > characters.


    The code presented attempts to interpret the file contents as a decimal
    specification of an 'unsigned short' value.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jul 3, 2004
    #6

  7. > std::fstream I("C:\\File.ext", ios_base::binary);
    > unsigned short a;
    > I.read(static_cast<char*>(&a), sizeof a);
    >


    Sorry reinterpret_cast not static_cast, or even a C style cast would do.

    john
    John Harrison, Jul 3, 2004
    #7
  8. On Sat, 3 Jul 2004 15:52:14 +0100, Fraser Ross
    <fraserATmembers.v21.co.unitedkingdom> wrote:

    >
    > "John Harrison"
    >> >
    >> >> What are the contents of C:\File.ext?
    >> > Its unimportant. The file only has to have at least sizeof(unsigned
    >> > short)
    >> > characters.

    >>
    >> No that is incorrect, is has to be text which can be converted to an
    >> integer, optionally preceded by whitespace.
    >>
    >> You seem to think you are doing a binary read of sizeof(unsigned short)
    >> bytes but that is not the case. You are reading text and that text must
    >> be
    >> a number.
    >>
    >> This is how you should do a binary read (minus error checking)
    >>
    >> std::fstream I("C:\\File.ext", ios_base::binary);
    >> unsigned short a;
    >> I.read(static_cast<char*>(&a), sizeof a);
    >>
    >> Use read for binary reads not >>.

    >
    > Thanks. Two other questions I've are: Why use ios_base and not ios?
    > Can a
    > use of read as above not be suitable for some systems?
    >
    > Fraser.
    >


    ios_base is standard but ios will also work since ios is derived from
    ios_base. Some old books will say ios since ios_base is a newer addition
    to C++.

    ios_base defines all the thing that apply to all types of streams. ios
    defines things that only apply to character streams, and there is also
    wios which defines things that only apply to wide character streams.

    read is suitable for all systems but the results on one system will differ
    from results on another system. You cannot take the same file and the same
    program and run them on two different systems and expect to get the same
    results. Similarly you cannot take a file which has been created using
    write on one system and then use read on another system and be sure to get
    the same data back. It might work, it might not. Basically binary I/O is
    not portable.

    john
    John Harrison, Jul 3, 2004
    #8
  9. "Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote:

    > #include <fstream>
    > #pragma hdrstop
    > #include <condefs.h>
    > #pragma argsused
    > int main(int argc, char* argv[])
    > {
    > std::fstream I;
    > I.open("C:\\File.ext");
    > if (I.is_open()) {
    > unsigned short a;
    > I >> a;
    > return 0;
    > }
    > else
    > return 1;
    > }
    >
    >
    > Is there anything wrong with this?


    I wouldn't use an fstream to just read text. I tried
    your program, and it read from a text file with ASCII
    representations of numbers in it, but it also corrupted
    the file, adding '3's to the beginning.

    Use an ifstream instead.

    > Why does the reading function fail?


    Probably because the file you're trying to open and
    read from either doesn't exist, or it doesn't contain
    ASCII representations of short integers separated by
    white spaces.

    Here's an improved version of your program:


    // ======= C:\test.txt =======
    34 92 61485 2 817


    // ======= inserter-test.cpp =======
    #include <iostream>
    #include <fstream>
    using std::ios_base;
    using std::cout;
    using std::cerr;
    using std::endl;
    using std::ifstream;
    int main()
    {
    ifstream I ("C:\\test.txt");
    if (!I.good() || !I.is_open())
    {
    cerr << "Error: Couldn't open file!" << endl;
    exit(666);
    }
    unsigned short int a = 0;
    while (1)
    {
    I >> a;
    if (I.bad())
    {
    cerr << "Error: Input bad!" << endl;
    I.close();
    exit(666);
    }
    else if (I.eof())
    {
    cout << "End of file." << endl;
    break;
    }
    else if (I.fail())
    {
    cerr << "Error: Input fail!" << endl;
    I.close();
    exit(666);
    }
    else
    {
    cout << "Number is: " << a << endl;
    }
    }
    I.close();
    return 0;
    }


    // ======= RESULTS: =======
    wd=C:\C\test
    %inserter-test
    Number is: 34
    Number is: 92
    Number is: 61485
    Number is: 2
    Number is: 817
    End of file.


    --
    Cheers,
    Robbie Hatley
    Tustin, CA, USA
    email: lonewolfintj at pacbell dot net
    web: home dot pacbell dot net slant earnur slant
    Robbie Hatley, Jul 3, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. slide_o_mix
    Replies:
    0
    Views:
    407
    slide_o_mix
    Oct 15, 2003
  2. Alex
    Replies:
    0
    Views:
    379
  3. Per
    Replies:
    2
    Views:
    401
    Mike Wahler
    Sep 1, 2004
  4. jalkadir
    Replies:
    5
    Views:
    447
    Thomas Maeder
    Oct 19, 2005
  5. ghager
    Replies:
    6
    Views:
    612
    ghager
    Jan 19, 2006
Loading...

Share This Page