null-terminated

string array with garbage character at end

不问归期 提交于 2019-12-17 06:53:49
问题 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 (/*

Java BufferedReader for zero-terminated strings

你离开我真会死。 提交于 2019-12-13 16:51:16
问题 I need to read zero-terminated strings from InputStream in Java. Is there similar to BufferedReader.readLine() method for reading zero-terminated strings? 回答1: No. Java doesn't recognise a zero-terminated string as such. You'll have to read the InputStream and look for a 0 byte. Note that this doesn't address the issue of character-encoding. The InputStream will give you the stream of bytes, and you'll then have to encode to characters via a Reader . If you have a multi-byte character

Is it Safe to strncpy Into a string That Doesn't Have Room for the Null Terminator?

拟墨画扇 提交于 2019-12-13 00:29:58
问题 Consider the following code: const char foo[] = "lorem ipsum"; // foo is an array of 12 characters const auto length = strlen(foo); // length is 11 string bar(length, '\0'); // bar was constructed with string(11, '\0') strncpy(data(bar), foo, length); cout << data(bar) << endl; My understanding is that string s are always allocated with a hidden null element. If this is the case then bar really allocates 12 characters, with the 12 th being a hidden '\0' and this is perfectly safe... If I'm

Sending Message Over TCP in Swift (NULL terminated)

浪尽此生 提交于 2019-12-12 04:30:29
问题 I am using swift socket library with the following code: let client:TCPClient = TCPClient(addr: "127.0.0.1", port: 8080) var (success,errmsg)=client.connect(timeout: 1) if success{ var (success,errmsg)=client.send(str:"|~\0" ) if success{ let data=client.read(1024*10) if let d=data{ if let str=String(bytes: d, encoding: NSUTF8StringEncoding){ print(str) } } }else{ print(errmsg) } }else{ print(errmsg) } The code works great but my problem is that my server gets the data without null-terminator

sgetn Doesn't Null Terminate String

你。 提交于 2019-12-10 22:39:23
问题 sgetn Takes a char* for it's first argument and writes characters to it. It does not write a trailing '\0' to the char* . This behavior seems to be inconsistent with every other time that I can find a char* written to. However, it is consistent across Clang, gcc, and Visual Studio, so I can't believe it's a bug that all the compilers have. Is there a reason that the standard doesn't require the trailing '\0' to the char* ? [Live Example] 回答1: Because it can be used to read arbitrary data, not

Using arrays of character strings: arrays of pointers - Are they like multidimensional arrays?

99封情书 提交于 2019-12-10 17:37:12
问题 I've been reading C++ for dummies lately and either the title is a misnomer or they didn't count on me. On a section about utilizing arrays of pointers with characters strings they show a function on which I've been completely stumped and don't know where to turn. char* int2month(int nMonth) { //check to see if value is in rang if ((nMonth < 0) || (nMonth > 12)) return "invalid"; //nMonth is valid - return the name of the month char* pszMonths[] = {"invalid", "January", "February", "March",

Special characters \0 {NUL} in Java

落爺英雄遲暮 提交于 2019-12-10 03:28:45
问题 How to replace \0 (NUL) in the String? String b = "2012yyyy06mm"; // sth what i want String c = "2\0\0\0012yyyy06mm"; String d = c.replaceAll("\\\\0", ""); // not work String e = d.replace("\0", ""); // er, the same System.out.println(c+"\n"+d+"\n"+e); String bb = "2012yyyy06mm"; System.out.println(b.length() + " > " +bb.length()); The above code will print 12 > 11 in console. Oops, What happened? String e = c.replace("\0", ""); System.out.println(e); // just print 2(a bad character)2yyyy06mm

Why strrchr() returns `char*` instead of `const char*`?

白昼怎懂夜的黑 提交于 2019-12-07 02:21:53
问题 The function char* strrchr(const char *str, int ch) returns a pointer ( char* ) within str ( const char * ) where the last occurrence of ch is located. So we can write the following code without any cast: #include <string.h> int main() { const char CONSTSTR[] = "foo/bar/foobar.txt"; char *ptr = strrchr (CONSTSTR, '/'); *ptr++ = 'B'; *ptr++ = 'A'; *ptr++ = 'D'; } What is the advantage to return char* instead of const char* ? EDIT: As a Shafik Yaghmour pointed out, there are very good answers

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

坚强是说给别人听的谎言 提交于 2019-12-06 14:28:40
问题 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"

How to deal with buffered strings from C in Swift?

时光总嘲笑我的痴心妄想 提交于 2019-12-06 08:44:31
问题 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