Thread: I need to change this program I wrote, from a struct format to a class

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by jimblumberg View Post
    What makes you think it won't work with a single character?

    Jim
    Well the file reading logic is incorrect. Because EOF is a condition, not a character. After reading the first byte of a 1 byte file, EOF will be set to true and the main loop will not execute. Also, if the file is empty the file stream will enter a fail state and EOF should return true immediately with the same effect: the loop will not execute.. thus "eating" a character. It would be simpler to use

    Code:
    while (cin << input) { blah; blah; blah; }
    in a C++ program.

    I know because coding a reliable input processing system, even as simple as this, is not that straightforward unless you understand the error conditions that can occur while reading from the file.

    EDIT:

    Ok, say the input file is 1 byte. Then, after executing the code

    Code:
    char c;
    cin << c; // or whatever wierd C++ thing to read a byte from a file LOL
    The EOF condition will be set to true. And the main loop will not execute. Also, if the file is empty, the cin << inFile will set the input stream into a fail state, and should also set EOF, and the main loop will not execute either way.

    I will note that the cin and cout operators << and >> are highly non-intuitive and confusing because, despite using them hundreds of times in practice, I still know that my usage here is probably incorrect ... due to its non-intuitive nature.

    I mean really wouldn't it be

    Code:
    char c = '\0';
    c << cin;
    to read a byte?

    Or is it

    Code:
    cin >> c;
    Or is there really any sense at all to it.. not really.. just learn the definitions I suppose. I myself would highly prefer something like

    Code:
    char c = fgetch(stdin);
    which, I think, makes its pretty damn clear whats happening there...
    Last edited by MacNilly; 11-28-2016 at 06:57 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. So I wrote a class.
    By brewbuck in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 12-13-2008, 06:22 PM
  2. Can someone look over the program I wrote?
    By brooklyn in forum C++ Programming
    Replies: 10
    Last Post: 04-16-2006, 07:23 AM
  3. Change data format from CString to int
    By ooosawaddee3 in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2002, 10:43 AM
  4. Qusetion on Class/Struct program
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-05-2002, 12:04 AM
  5. Can anyone change this into C format?
    By Wizard_D in forum C++ Programming
    Replies: 6
    Last Post: 12-19-2001, 08:49 AM

Tags for this Thread