-4

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 8false? 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?

user2561614
  • 51
  • 1
  • 6
  • 3
    `sizeof(a)` doesn't give you the number of characters, it gives you the size of a pointer because `a` is a pointer. Use `strlen` to get the number of characters when dealing with C-strings. – Tuntuni Jul 17 '13 at 16:18
  • To address your title and first sentence: A C++ "byte" is not necessarily the same as a filesystem byte. When used in the C++ Standard, "byte" means "the size of a `char` variable". – Ben Voigt Jul 17 '13 at 16:18
  • Asked some 20 minutes ago. You guys really can't be bothered to use the search, can you? Possible duplicate of [sizeof() function in C](http://stackoverflow.com/questions/17704923/sizeof-function-in-c) –  Jul 17 '13 at 16:30
  • Yes, I should have defined as char a[]="blabla" to get the correct length by sizeof.It was just a hurried example.Sorry for that. If byte is not neccessarily the same as a filesytem byte, what exactly does a filesystem byte represent and what do you suggest or can you give an example of portable code in binary mode to read and write data correctly? – user2561614 Jul 17 '13 at 16:48
  • @Tuntuni: The original version of the question defined `a` as a pointer. It's been edited to `char a[]="asdfghjkkll";`; with that change, `sizeof a` does yield the number of characters (including the terminating `'\0'`). – Keith Thompson Jul 17 '13 at 17:42
  • possible duplicate of [What does sizeof(&arr) return?](http://stackoverflow.com/questions/15177420/what-does-sizeofarr-return) – Grijesh Chauhan Jul 17 '13 at 19:09
  • Not at all. I have no problem with sizeof operator.My point is aligning of data in both stream byte and char variable if there is such a distinction.Thanks. – user2561614 Jul 17 '13 at 19:24

1 Answers1

2

A language standard can't really specify what the filesystem does - it can only specify how the language interacts with it. The C and C++ standards also don't address anything to do with interoperability or communication between different implementations. In other words, there isn't a general answer to this question except to say that:

  • the VAST majority of systems use 8-bit bytes
  • the C and C++ standard require that char is at least 8 bits
  • it is very likely that greater-than-8-bit systems have mechanisms in place to somehow utilize (or at least transcode) 8-bit files.
Casey
  • 41,449
  • 7
  • 95
  • 125