fstream::get(char&) repeating last character

Discussion in 'C++' started by kieran@cyrocom.co.uk, May 29, 2005.

  1. Guest

    Hi,
    I'm using fstream.get to read in a character from a file, then print it
    on the screen. I have a file called test.log that contains "Hello,
    World!", but when I try and print the contents out on the screen I get
    "Hello, World!!". The following is the code (please ignore all
    wxWidgets classes):

    void SystemBackupFrm::compress()
    {
    using namespace std;
    const wxString dir = "C:\\Backups\\";

    wxFFileOutputStream out(dir + "files.zip");
    wxZipOutputStream zip(out);
    wxTextOutputStream data(zip);


    fstream file1("C:\\test.log", ios::in );
    zip.PutNextEntry("C:\\test.log");
    char t;


    while(file1.good())
    {
    file1.get(t);
    cout << t ;
    data << t;
    }
    file1.close();

    }

    Cheers,
    Kieran
    , May 29, 2005
    #1
    1. Advertising

  2. * :
    > I'm using fstream.get to read in a character from a file, then print it
    > on the screen. I have a file called test.log that contains "Hello,
    > World!", but when I try and print the contents out on the screen I get
    > "Hello, World!!". The following is the code (please ignore all
    > wxWidgets classes):
    >


    [extranous code removed]
    > void SystemBackupFrm::compress()
    > {
    > using namespace std;
    > fstream file1("C:\\test.log", ios::in );


    Strong hint: make that

    "c:/test.log"

    Windows accepts both forward and backward slash at the API level.
    You only need those awkward backward slashes for the user interface,
    and then only if you elect to uphold the illusion of backslashisism.
    With forward slash you can use a single convention throughout the
    C++ code, especially in #include directives.

    > char t;
    >
    > while(file1.good())
    > {
    > file1.get(t);
    > cout << t ;
    > }
    > file1.close();
    > }


    When 'get' fails you still output the the content of 't', which
    hasn't been changed.

    --
    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, May 29, 2005
    #2
    1. Advertising

  3. Guest

    Thanks,
    I stupidly thought it was something to do with fetching the character
    itself. Also thanks for the advice on forward and back slashes,
    Cheers,
    Kieran
    , May 29, 2005
    #3
  4. Jon Bell Guest

    In article <>,
    <> wrote:
    >
    > while(file1.good())
    > {
    > file1.get(t);
    > cout << t ;
    > data << t;
    > }


    good() returns "false" only *after* you have *tried* and *failed* to read
    past the end of the file. A correct way to write your input loop is as
    follows:

    while (file1.get(t))
    {
    cout << t;
    data << t;
    }

    In a boolean context, get() (like all other standard C++ input operations)
    evaluates as "true" if the input succeded, and "false" if it failed.

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, May 29, 2005
    #4
    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. Armando
    Replies:
    6
    Views:
    745
    Armando
    Jan 29, 2004
  2. Daniel Mark
    Replies:
    6
    Views:
    70,740
    Tim Chase
    Sep 28, 2006
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,034
    Ian Collins
    May 9, 2006
  4. gert
    Replies:
    20
    Views:
    1,154
  5. Johny
    Replies:
    8
    Views:
    389
Loading...

Share This Page