Thread: Fstream problem.

  1. #1
    deletedforumuser
    Guest

    Fstream problem.

    How come im not getting the value for health?

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    
    using namespace std;
    
    
    int main()
    {
    
    	string filebuffer = "";
    
    	int gold;
    	int health;
    
    
    	ifstream file("file.add");
    
    	file.flags(ios::hex);
    	file >> gold;
    	file >> health;
    
    	cout << (int*)gold<<endl;
    
    
    	cout <<gold<<endl;
    	cout <<health<<endl;
    	return 0;
    }
    Gold is receiving the values, but health isn't? What is the problem?
    I'm having this problem for memory adress stuff for about 2 hours now...

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What does the file say? What are you getting that you shouldn't be getting, or vice versa?

  3. #3
    deletedforumuser
    Guest
    Code:
    002CFE40
    002CFE40
    Health is not even receiving a value.
    Last edited by kevinawad; 10-18-2008 at 10:22 PM.

  4. #4
    deletedforumuser
    Guest
    file.flags(ios::hex);
    file >> gold;
    file >> health;

    Gold is receiving the first line...
    Health is supposed to receive the second line.

    I think it's a problem with Hex values.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I've always wondered about the difference between flags and setf, and now I know: flags will clear all the other flags, things like skipws which are important.

    Change the flags line to
    Code:
    file.setf(ios::hex, ios::basefield);
    Last edited by tabstop; 10-18-2008 at 10:52 PM.

  6. #6
    deletedforumuser
    Guest
    Now, im getting wrong values for Gold.

    And still not getting the value for health

  7. #7
    deletedforumuser
    Guest
    Try this code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    
    using namespace std;
    
    
    int main()
    {
    
    	string filebuffer = "";
    
    	int gold;
    	int health;
    
    
    	ifstream file("file.add");
    
    	file.setf(ios::hex | ios::basefield);
    	file >> gold;
    	file >> health;
    
    	cout << (int*)gold<<endl;
    
    
    	cout <<gold<<endl;
    	cout <<health<<endl;
    	return 0;
    }

    Make a file.add and put this in it:

    Code:
    002CFE40
    5
    It wont work.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    file.setf(ios::hex | ios::basefield);
    That's the ugliest comma I've ever seen.

  9. #9
    deletedforumuser
    Guest
    Haha, oops

  10. #10
    deletedforumuser
    Guest
    Thank you so much ...thank you thank you...it works......will never do a comma error next time haha.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Fstream Problem
    By Xterria in forum C++ Programming
    Replies: 2
    Last Post: 08-25-2001, 11:03 PM