What I understood about char
type from a few questions asked here is that it is always 1
byte in C++, but number of bits can vary from system to system.
sizeof()
operator uses char
as a unit so sizeof(char)
is always 1
in bytes of C++.(which takes number of bits of smallest unit of address of local machine) If when using file functions of fstream()
in binary mode, we directly read and write from/to an address of any variable in RAM, the size of variable as smallest unit of data written to file should be in size of the value read from RAM and for one read from file it is vice-versa. Then can we say that data may not be written 8
by 8
in bits if something like this is tried:
ofstream file;
file.open("blabla.bin",ios::out|ios::binary);
char a[]="asdfghjkkll";
file.seekp(0);
file.write((char*)a,sizeof(a)-1);
file.close();
Unless char
is always used in bytes existing standard 8
bits, what happens if a heap of data is written to file in a 16 bit machine and is read in a 32 bit machine? Or should I use OS-dependent text mode? If not, and I misunderstood what is truth?
Edit : I have corrected my mistake.
Thanks for warning.
Edit2: My system is 64
bit but I get number of bits of char
type as 8
.What is wrong? Is the way I get the result of 8
false?
I got a 00000...
by shifting a char
variable more than possible size of it with bitwise operators.After guaranteeing that all bits of the variable is zero, I got a 111...
by inverting it. And shifted until it become zero.If we shift it its size time
, we get a zero, so we can get number of bits from indice of the loop terminated below.
char zero,test;
zero<<=64; //hoping that system is not more than 64 bit(most likely)
test=~zero; //we have a 111...
int i;
for(i=0; test!=zero; i++)
test=test<<1;
Value of variable of i
after the loop is number of bits in char
type.According to this, the result is 8
.
My last question is:
Are filesystem byte and char
type different data types because how computer adresses pointers in file stream is different from standart char
type which is at least 8
bits?
So, exactly what is going on the background?
Edit3: Why these minuses? What is my mistake? Isn't the question clear enough? Maybe my question is stupid but why there is no any response related to my question?