none
ifstream does not work properly

    Question

  • Hello,
    I have a problem with ifstream but don't know how to tell. So I attach a picture.

    The ifile correctly opens the file as you can see but when it want to read the first line and put it in n_polygons, it doesn't read it correctly and it somehow malfunctioning. The first line in the file is an integer number (19) but I don't know why ifstream does not work properl??
    [IMG]http://i37.tinypic.com/9fqf6x.jpg[/IMG]
    http://i37.tinypic.com/9fqf6x.jpg

    "I use VC++ 8.0"
    • Edited by mahmoodn Tuesday, October 14, 2008 4:02 PM
    Tuesday, October 14, 2008 3:54 PM

Answers


  • The stream extraction failed, as indicated by _Mystate=2
    in your pic. You should be testing for failure of the extraction
    after each attempt. Never just assume that an extraction or
    read or get or put or write or seek, etc. will succeed.

    There is some ambiguity in your description of the contents of
    the file. When you say the first line contains 19 do you mean it
    has the two ANSI (or ASCII) characters 1 (0x31 ) and 9 (0x39 )?
    If so, then that should be converted to an int automatically by the
    extraction operations (formatted input). On the other hand, if it
    contains a decimal 19 (0x13) then you have to open the file in
    binary mode and get or read the number.

    How was the file created? Via a text editor or as output from
    another program which wrote an int to the file?

    - Wayne

    • Marked as answer by mahmoodn Thursday, October 16, 2008 5:58 AM
    Wednesday, October 15, 2008 1:25 PM

All replies

  • I received the alert email says that "nobugs" has replied, but I don't know why it isn't here!!

    This is what he says:
    Open the file in Notepad.exe.  Does it say "19"?  Then you'll need to read the number into a string and convert it to an integer.


    Hans Passant.

    The Microsoft Developer Network

    The first line is 19 and not "19" which is a string, so there is no need to convert string to int. I am still struggling....


    "I use VC++ 8.0"
    Wednesday, October 15, 2008 10:19 AM
  • Did you try using a string?
    Say,
    std::string sData;
    iFile >> sData;

    May be you would get a string and then you need to convert that to a number.

    Hope this helps.

    • Edited by Rajesh V Wednesday, October 15, 2008 1:27 PM EDIT
    Wednesday, October 15, 2008 11:44 AM

  • The stream extraction failed, as indicated by _Mystate=2
    in your pic. You should be testing for failure of the extraction
    after each attempt. Never just assume that an extraction or
    read or get or put or write or seek, etc. will succeed.

    There is some ambiguity in your description of the contents of
    the file. When you say the first line contains 19 do you mean it
    has the two ANSI (or ASCII) characters 1 (0x31 ) and 9 (0x39 )?
    If so, then that should be converted to an int automatically by the
    extraction operations (formatted input). On the other hand, if it
    contains a decimal 19 (0x13) then you have to open the file in
    binary mode and get or read the number.

    How was the file created? Via a text editor or as output from
    another program which wrote an int to the file?

    - Wayne

    • Marked as answer by mahmoodn Thursday, October 16, 2008 5:58 AM
    Wednesday, October 15, 2008 1:25 PM
  • beautiful.... I entered values in the file manually with notepad++. What I remember is
    that I mistakenly save the file without extension (all types *.*) and then when I noticed I
    manually add .txt extension.

    Then I followed Rajesh adviced and when watching sData, the value was:
    +        sData    "19"    std::basic_string<char,std::char_traits<char>,std::allocator<char> >

    So I suspected to the editor, but I was unable to locate exactly the problem. With wayne's solution
    it is now correct.
    "I use VC++ 8.0"
    • Edited by mahmoodn Thursday, October 16, 2008 5:58 AM
    Thursday, October 16, 2008 5:56 AM