fstream with numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class something
{
public:
    int zero;
    int med;
    char one;
    friend std::ostream& operator << (std::ostream &stream, const something& thing)
	{
		stream << thing.zero << " " << thing.med << thing.one;
		return stream;
	};
    friend std::istream& operator >> (std::istream &stream, something& thing)
	{
		stream >> thing.zero >> thing.med >> thing.one;
		return stream;
	};
};

Why does the input both, produce erroneous values and fail() unless I have the " " when dealing with integers?
I am using files. Is there a trick I'm not aware of? ;)
(btw, this is a dummy class)
because if you have eg: 1 and 2 the output would be 12, which is recognised as a single integer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    something nothing;
    nothing.zero = 7000;
    nothing.med = 8500;
    nothing.one = 'a';
        std::cout << nothing << std::endl;
        file.seekp(0,GFA_OFFSET_BEGIN);
        file << nothing;

        file.seekg(0,GFA_OFFSET_BEGIN);
        if(file.fail())
            std::cout << "\nFile opened, but an error occured during write.\n";
        else
            std::cout << "\nThe file was written to.\n";
        file.clear();
        file >> nothing;
        if(file.fail())
            if(file.bad())
                std::cout << "\nThe file could not be read.\n";
            else
                std::cout << "\nThe file contained the wrong type.\n";
        else
            std::cout << "\nThe file was read.\n";
        file.close();
        std::cout << nothing.zero << std::endl << nothing.med<< std::endl << nothing.one << std::endl;

File
70008500a
Console
70008500a

The file was written to.

The file contained the wrong type.
70008500
8500
a


Then why does it fail() and return 8500?
Last edited on
The value of nothing.med and nothing.one being printed is the value you initialized them with
on lines 3-4 (since valid data was not read, their values were left unmodified).
Ah, I see. Thanks. So is it typical to put " " between integers? Is there another standard to use? '\0' didn't work. =[
You can put whichever character you like but you'll need to use getline instead of >> then.
>> stops reading when a whitespace is found ( ' ', '\t', '\n', '\r', '\v' )
Topic archived. No new replies allowed.