null-terminated

C++: Store read binary file into buffer

谁都会走 提交于 2019-12-04 22:52:56
问题 I'm trying to read a binary file and store it in a buffer. The problem is, that in the binary file are multiple null-terminated characters, but they are not at the end, instead they are before other binary text, so if I store the text after the '\0' it just deletes it in the buffer. Example: char * a = "this is a\0 test"; cout << a; This will just output: this is a here's my real code: this function reads one character bool CStream::Read (int * _OutChar) { if (!bInitialized) return false; int

How to print a string using printf without it printing the trailing newline

扶醉桌前 提交于 2019-12-04 19:37:48
I'm trying to print some strings using printf() but they are null terminated having trailing newline and that messes with the formating: printf("The string \"%s\" was written onto the file \"%s\"", str, fname); Say the string contains "The Racing car." and the file name is "RandomText1.txt" This prints: The string "The Racing car. " was written onto the file "RandomText1.txt " However I want it to print in just one line: The string "The Racing car." was written onto the file "RandomText1.txt" I know I can modify the strings to get rid of the null terminator newline but I'd like a way, if

Null character in strings

别来无恙 提交于 2019-12-04 17:07:34
问题 Consider this string: var s = "A\0Z"; Its length is 3, as given by s.length . Using console.log you can see the string isn't cut and that s[1] is "" and s.charCodeAt(1) is 0 . When you alert it in Firefox, you see AZ . When you alert it in Chrome/Linux using alert(s) , the \0 terminates the string and you see A . My question is: what should browsers and Javascript engines do? Is Chrome buggy here? Is there a document defining what should happen? As this is a question about standard, a

Why are C#/.Net strings length-prefixed and null terminated?

浪子不回头ぞ 提交于 2019-12-04 16:56:56
问题 After reading What's the rationale for null terminated strings? and some similar questions I have found that in C#/.Net strings are, internally, both length-prefixed and null terminated like in BSTR Data Type. What is the reason strings are both length-prefixed and null terminated instead of eg. only length-prefixed? 回答1: Length prefixed so that computing length is O(1) . Null terminated to make marshaling to unmanaged blazing fast (unmanaged likely expects null-terminated strings). 回答2: Here

How to deal with buffered strings from C in Swift?

假如想象 提交于 2019-12-04 12:43:15
I'm working with libxml2's sax parser to read large xml files. Most callback handlers are provided a NULL terminated char pointer. Using String.fromCString these can be converted to a regular string in Swift. However sax uses a buffer for reading the bytes, so one of the callbacks ( characters ) might be called with part of a string, namely the size of the buffer. This partial string might even start/end halfway a Unicode code point. The callback will be called multi times, until the complete string is provided (in chunks). I'm thinking of either concatenating all chunks until the complete

Warning comparison between pointer and integer

一个人想着一个人 提交于 2019-12-03 15:54:12
问题 I am getting an error when I iterate through the character pointer and check when the pointer reaches the null terminator. const char* message = "hi"; //I then loop through the message and I get an error in the below if statement. if (*message == "\0") { ...//do something } The error I am getting is: warning: comparison between pointer and integer ('int' and 'char *') I thought that the * in front of message dereferences message, so I get the value of where message points to? I do not want to

How can a file contain null bytes?

自古美人都是妖i 提交于 2019-12-03 14:43:09
问题 How is it possible that files can contain null bytes in operating systems written in a language with null-terminating strings (namely, C)? For example, if I run this shell code: $ printf "Hello\00, World!" > test.txt $ xxd test.txt 0000000: 4865 6c6c 6f00 2c20 576f 726c 6421 Hello., World! I see a null byte in test.txt (at least in OS X). If C uses null-terminating strings, and OS X is written in C, then how come the file isn't terminated at the null byte, resulting in the file containing

C++: Store read binary file into buffer

微笑、不失礼 提交于 2019-12-03 14:26:39
I'm trying to read a binary file and store it in a buffer. The problem is, that in the binary file are multiple null-terminated characters, but they are not at the end, instead they are before other binary text, so if I store the text after the '\0' it just deletes it in the buffer. Example: char * a = "this is a\0 test"; cout << a; This will just output: this is a here's my real code: this function reads one character bool CStream::Read (int * _OutChar) { if (!bInitialized) return false; int iReturn = 0; *_OutChar = fgetc (pFile); if (*_OutChar == EOF) return false; return true; } And this is

Warning comparison between pointer and integer

余生颓废 提交于 2019-12-03 06:09:15
I am getting an error when I iterate through the character pointer and check when the pointer reaches the null terminator. const char* message = "hi"; //I then loop through the message and I get an error in the below if statement. if (*message == "\0") { ...//do something } The error I am getting is: warning: comparison between pointer and integer ('int' and 'char *') I thought that the * in front of message dereferences message, so I get the value of where message points to? I do not want to use the library function strcmp by the way. It should be if (*message == '\0') In C, simple quotes

How can a file contain null bytes?

心不动则不痛 提交于 2019-12-03 04:26:45
How is it possible that files can contain null bytes in operating systems written in a language with null-terminating strings (namely, C)? For example, if I run this shell code: $ printf "Hello\00, World!" > test.txt $ xxd test.txt 0000000: 4865 6c6c 6f00 2c20 576f 726c 6421 Hello., World! I see a null byte in test.txt (at least in OS X). If C uses null-terminating strings, and OS X is written in C, then how come the file isn't terminated at the null byte, resulting in the file containing Hello instead of Hello\00, World! ? Is there a fundamental difference between files and strings? Null