I use cout to write an integer value to a .txt file. And sure, it works. But what is stored in the file? |
Let's say all you take is an
int
, n ==
255 and put it into a text file, like so:
1 2 3 4 5 6 7 8 9 10 11
|
#include <fstream>
using namespace std;
int main()
{
int n = 255;
ofstream output("myFile.txt"); //of course this would contain the full path
output<<n;
output.close();
}
| |
The value of n, in the program, in binary representation would be the following:
1 2
|
31 30 ... 8 7 6 5 4 3 2 1 0
0 0 ... 0 1 1 1 1 1 1 1 1
| |
where the top numbers represent the bit position (least significant on the right). You can see from calculation that this is so because
1 2 3 4 5 6 7 8
|
255 = 27 (128) +
26 (64) +
25 (32) +
24 (16) +
23 (8) +
22 (4) +
21 (2) +
20 (1)
| |
while "myFile.txt" would contain the digits of n in character representation (one byte each), as per the following:
ASCII '2' (50)
0 0 1 1 0 0 1 0
ASCII '5' (53)
0 0 1 1 0 1 0 1
ASCII '5' (53)
0 0 1 1 0 1 0 1
End of File (4)
0 0 0 0 0 1 0 0
What happens when I use cin to extract that data to a variable of type string in my program? |
A
std::string
maintains a dynamic
char*
. So, say you enter the characters 123456 into the console. The string would dynamically allocate an array of (at least) 7 bytes to be pointed to by the
char*
. When all is said and done, the
char*
would contain some memory location, M, where the memory starting at M would look like the following:
1 2 3 4 5 6 7 8 9 10
|
M -> 0 0 1 1 0 0 0 1 ( '1', ASCII 49)
M + 1 byte -> 0 0 1 1 0 0 1 0 ( '2', ASCII 50)
M + 2 bytes -> 0 0 1 1 0 0 1 1 ( '3', ASCII 51)
M + 3 bytes -> 0 0 1 1 0 1 0 0 ( '4', ASCII 52)
M + 4 bytes -> 0 0 1 1 0 1 0 1 ( '5', ASCII 53)
M + 5 bytes -> 0 0 1 1 0 1 1 0 ( '6', ASCII 54)
M + 6 bytes -> 0 0 0 0 0 0 0 0 ('\0', ASCII 0)
M + 7 bytes -> ?
M + 8 bytes -> ?
...
| |
The
std::string
would also contain some other internals.
And what about to a variable of type int? |
This is when some magic happens. Say you enter 123 into the console and do cin>> into an int variable. What happens, is it figures out that the character '1' is in the hunderds position and thus it adds 1 x 100 to your int (initially zero). It then figures out that the character '2' is in the tens position and it adds 2 x 10 to your int. Finally, it determines that the character '3' is in the ones position and it adds 3 x 1 to your int. It does nothing with the null terminator character.
I am using getline() to extract the data from the .txt. But I know I can't use that to put the numbers into int like I could with cin |
What you could do in this case is use getline and put the line from the file in a string. You could then use a stringstream to convert the string into an int.
http://www.cplusplus.com/reference/iostream/stringstream/
You could also use a binary file instead of a text file. That way, when you write an int to the file (4 bytes) you would then be able to read those 4 bytes back and put it directly into an int. No conversion necessary.