I know this is a stupid question but I don't understand this problem. Could anyone please explain to me?  I know that a char is 1 byte. I created a file sample.txt and fill in "1234" I used fseek and ftell to print the file size. The output is 4 bytes. This means each of char in the string "1234" is 1 byte.  But when I write like this printf("%d", sizeof('4')); The output is 4. So the char 4 is stored with 4 bytes.  So what are differences between the char 4 that read from the file and char 4 in the source?  *** My question was marked duplicated here:   But it is not. The other topic doesn't explain why computer treats the char in the file as 1 byte each.
7720
In C++, sizeof('a') == sizeof(char) == 1. This makes intuitive sense, since 'a' is a character literal, and sizeof(char) == 1 as defined by the standard.  In C however, sizeof('a') == sizeof(int). That is, it appears that C character literals are actually integers. Does anyone know why? I can find plenty of mentions of this C quirk but no explanation for why it exists.
69127
I'm looking for detailed information regarding the size of basic C++ types. I know that it depends on the architecture (16 bits, 32 bits, 64 bits) and the compiler.  But are there any standards for C++?  I'm using Visual Studio 2008 on a 32-bit architecture. Here is what I get:  char  : 1 byte short : 2 bytes int   : 4 bytes long  : 4 bytes float : 4 bytes double: 8 bytes   I tried to find, without much success, reliable information stating the sizes of char, short, int, long, double, float (and other types I didn't think of) under different architectures and compilers.
109558
Are these two equivalent?  char * aString = "This is a string.";  char aString[] = "This is a string.";  From my understanding, C++ stores strings by their addresses, so the pointer is getting a valid address. I assume the strings are also stored as type char? But how does it know how much memory to allocate for the string? Normally, char arrays need their array size preset. I believe the [] type arrays are counted by the compiler before compilation. Is this what happens for the string literal pointer as well?  On to my second question, what is the difference between these two:  int someNumber;  int * someNumber = new int;  The second is supposed to be dynamic allocation. If I understand the concept right, it allocates the memory as soon as it reads the declaration. But what happens in the standard declaration? Basically, I need to understand how dynamic allocation is advantageous over standard declarations.
86471
I have an empty file (only zeroes are in it) of size 9,0KB and I need to write another file (with size 1,1KB) to it, but the first file must not lose its size or the rest of its contents. So if the whole file is 00000000000000... now, I need to write second file in it and leave the zeroes as they are. I have tried to use dd, but I haven't succeed - file resizes.  dd if=out/one.img of=out/go.img   Does anybody know how can I do it?
84866
I wanted to use fstream to read a txt file.  I am using inFile &gt;&gt; characterToConvert, but the problem is that this omits any spaces and newline.  I am writing an encryption program so I need to include the spaces and newlines.  What would be the proper way to go about accomplishing this?
64766
When I make a call to the C string compare function like this:  strcmp("time","time")  It returns 0, which implies that the strings are not equal.  Can anyone tell me why C implementations seem to do this? I would think it would return a non-zero value if equal. I am curious to the reasons I am seeing this behavior.
2136
I am looking for a way to convert a long string (from a dump), that represents hex values into a byte array.  I couldn't have phrased it better than the person that posted .  But to keep it original, I'll phrase it my own way: suppose I have a string "00A0BF" that I would like interpreted as the  byte[] {0x00,0xA0,0xBf}   what should I do?  I am a Java novice and ended up using BigInteger and watching out for leading hex zeros. But I think it is ugly and I am sure I am missing something simple.
67482
What is the longest string that can be created in .NET? The docs for the String class are silent on this question as far as I can see, so an authoritative answer might require some knowledge of internals. Would the maximum change on a 64-bit system?  [This is asked more for curiosity than for practical use - I don't intend to create any code that uses gigantic strings!]
113831
Just something I ran into and couldn't think of a proper explanation. If I create an empty *.txt file on my PC and then look at its size, it shows 0. But how is that possible? I mean even if the file itself is empty, it still must have some size, just to store its own name. How can this be explained? (Non OS specific)
73598
I have a char array buffer that I am using to store characters that the user will input one by one.  My code below works but has a few glitches that I can't figure out:   when I execute a printf to see what's in Buffer, it does fill up but I get garbage characters at the end it won't stop at 8 characters despite being declared as char Buffer[8];   Can somebody please explain to me what is going on and perhaps how I could fix this? Thanks.  char Buffer[8]; //holds the byte stream int i=0;  if (/* user input event has occurred */)  {         Buffer[i] = charInput;         i++;          // Display a response to input         printf("Buffer is %s!\n", Buffer);  }   Output:   tagBuffer is 1┬┬w! tagBuffer is 12┬w! tagBuffer is 123w! tagBuffer is 1234! tagBuffer is 12345! tagBuffer is 123456=! tagBuffer is 1234567! tagBuffer is 12345678!  tagBuffer is 123456789!
24659
I have a string of digits, e.g. "123456789", and I need to extract each one of them to use them in a calculation. I can of course access each char by index, but how do I convert it into an int?  I've looked into atoi(), but it takes a string as argument. Hence I must convert each char into a string and then call atoi on it. Is there a better way?
32795
Why is it that scanf() needs the l in "%lf" when reading a double, when printf() can use "%f" regardless of whether its argument is a double or a float?  Example code:  double d; scanf("%lf", &amp;d); printf("%f", d);
9051
Check out the below program.   try {     for (String data : Files.readAllLines(Paths.get("D:/sample.txt"))){         String[] de = data.split(";");         System.out.println("Length = " + de.length);     } } catch (IOException e) {     e.printStackTrace(); }   Sample.txt:   1;2;3;4 A;B;; a;b;c;   Output:   Length = 4 Length = 2 Length = 3   Why second and third output is giving 2 and 3 instead of 4. In sample.txt file, condition  for 2nd and 3rd line is should give newline(\n or enter) immediately after giving delimiter for the third field. Can anyone help me how to get length as 4 for 2nd and 3rd line without changing the condition of the sample.txt file and how to print the values of de[2] (throws  ArrayIndexOutOfBoundsException)?
122420
I get a transmission over the network that's an array of chars/bytes. It contains a header and some data. I'd like to map the header onto a struct. Here's an example:  #pragma pack(1)  struct Header {     unsigned short bodyLength;     int msgID;     unsigned short someOtherValue;     unsigned short protocolVersion; };  int main() {     boost::array&lt;char, 128&gt; msgBuffer;     Header header;      for(int x = 0; x &lt; sizeof(Header); x++)         msgBuffer[x] = 0x01; // assign some values      memcpy(&amp;header, msgBuffer.data(), sizeof(Header));      system("PAUSE");          return 0; }   Will this always work assuming the structure never contains any variable length fields? Is there a platform independent / idiomatic way of doing this?  Note:  I have seen quite a few libraries on the internet that let you serialize/deserialize, but I get the impression that they can only deserialize something if it has ben previously serialized with the same library. Well, I have no control over the format of the transmission. I'm definitely going to get a byte/char array where all the values just follow upon each other.
20886
Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like getch()). I know there's a function in Windows for it, but I'd like something that is cross-platform.
112071
We had a discussion here at work regarding why fread() and fwrite() take a size per member and count and return the number of members read/written rather than just taking a buffer and size. The only use for it we could come up with is if you want to read/write an array of structures which aren't evenly divisible by the platform alignment and hence have been padded but that can't be so common as to warrant this choice in design. From :  The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr.   The  function  fwrite()  writes  nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.   fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters).  If an error occurs, or  the end-of-file is reached, the return value is a short item count (or zero).
27778
Is there an efficient way to create a file with a given size in Java?  In C it can be done with  (see ).   Most people would just write n dummy bytes into the file, but there must be a faster way. I'm thinking of  and also of …