Path: utzoo!utgpu!watserv1!watmath!att!dptg!ulysses!andante!princeton!udel!wuarchive!uunet!mcsun!cernvax!chx400!ethz!neptune!c!mneerach From: mneerach@c.inf.ethz.ch (Matthias Ulrich Neeracher) Newsgroups: comp.sys.mac.programmer Subject: Re: char strings and TextEdit Message-ID: <52@neptune.inf.ethz.ch> Date: 16 Jul 90 10:35:37 GMT References: <1990Jul15.085520.14428@gpu.utcs.utoronto.ca> Sender: news@neptune.inf.ethz.ch Reply-To: mneerach@c.inf.ethz.ch (Matthias Ulrich Neeracher) Organization: U. of Waterloo, Ontario Lines: 44 In article <1990Jul15.085520.14428@gpu.utcs.utoronto.ca> topix@gpu.utcs.utoronto.ca (R. Munroe) writes: >Why does the following work OK ... >char buffer[255] = "This is a test."; >TEInsert(&buffer, strlen(buffer), textHdl); >while the following does not? >char buffer[255]; >PtoCstr(buffer); >sprintf(buffer, "This is a test."); >CtoPstr(buffer); >TEInsert(&buffer, strlen(buffer), textHdl); In the first case, you are dealing with a C string and are giving a pointer to the first character and the correct length, which works fine. In the second case, you work with a *Pascal* string, i.e. you give a pointer to the length byte, which is wrong, and you are using strlen on a Pascal string which is *very* wrong, because strlen looks for a '\0' character and after using CtoPstr, no '\0' character is guaranteed to be around. So, leave out the CtoPstr and everything should work. Oh yes, and leave out the PtoCstr also: At best it does nothing (buffer is overwritten by the sprintf anyway). >Another minor question - how can I get a newline character from a string to >display properly? The following ... >TEHandle textHdl; >..... /* more stuff */ >char buffer[255] = "This is a test.\nSo is this."; >TEInsert(&buffer, strlen(buffer), textHdl); > >displays a non-printing character where the newline should be. I don't know about Think C, but I understand many compiler differentiate between '\n' (ASCII 10) and '\r' (ASCII 13). Try '\r', maybe this works. Matthias *************************************************************************** * Matthias Neeracher * I wouldn't recommend sex, drugs or insanity for * * mneerach@inf.ethz.ch * everyone, but they've always worked for me - HST * ***************************************************************************