CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2004
    Posts
    8

    how to read from text file

    Hey guys,

    I'm having trouble reading an integer from a text file.

    for example I have the following stored in a text file

    ------------ beginning of file------------excluding this line
    H
    88
    ------------ end of file------------excluding this line

    and there are no spaces after any of the above words, each word is stored in one line.

    I have an ifstream object that reads

    char charVar;
    int intVar;
    inFile.read(reinterpret_cast(&charVar),sizeof(char));
    cout <<"char value is : "<<charVar ;
    inFile.read(reinterpret_cast(intVar),sizeof(int));
    cout <<"int Var is : "<<intVar;

    the first one works fine, ..but I cant seem to read the integer I m getting some negative numberss

    and I have to use the ios::binay method to open the file,

  2. #2
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    What's with all the casting? I believe this should do what you want:
    Code:
    char charVar;
    int intVar;
    
    inFile >> charVar;
    inFile.ignore(); // Ignore the newline character!
    cout << "char value is : " << charVar ;
    
    inFile >> intVar;
    inFile.ignore(); // Ignore the newline character!
    cout << "int Var is : " << intVar;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured