Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I get rid of '^M' carriage returns when writing to file

Status
Not open for further replies.

Vovin

Programmer
Aug 24, 2003
63
GB
Hi,

I'm trying to append the following to a pdf file:
<OPENTAG>some text<ENDTAG>
The trouble is that when I use Writeln or Write it adds carriage return characters after each line (even though it's a single text string) which causes acrobat to complain that the file is corrupted.

Here is my code:
AssignFile(pdfFile, filename);
Append(pdfFile);
WriteLn(pdfFile, stringToAddToFile);
CloseFile(pdfFile);

How can I get rid of the carriage return? I'm a C++ programmer really and don't know much about Delphi. Is there any low level file functions that I can use that would give me more control over this?

Any help would be much appreciated.
 
a) Replace WriteLn with Write
b) Don't include any #13 and/or #10 in the stringToAddToFile

HTH
TonHu
 
Thanks TonHu but I tried using just Write and I get the same result (carriage returns are still added).

I'm having a look at TFileStream at the moment but can't get it to work at all. Does anyone know of any good tutorials on TFileStream for appending to a file?

I suppose I could write a C++ dll to do this for me but there must be a way to do it within Delphi. Isn't there?
 
hi

Are you sure there's no carriage returns in the string?

Before writing it to the file, try

NewStr := StrToAddToFile;
NewStr := StringReplace(NewStr,#13,'',[rfReplaceAll]);
NewStr := StringReplace(NewStr,#10,'',[rfReplaceAll]);
Write(pdfFile, NewStr);

Hope this helps. If not, you must have a stray character in there that's causing it.

Also, if you use Writeln, that will append the string to the current line and add carriage return to the end.

 
Thanks LucieLastic,

that got rid of the characters. I really don't know why they were in the first place.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top