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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing in files with VB: I Don't wan't "^M" written 1

Status
Not open for further replies.

LuckyStarr

Programmer
Sep 12, 2002
22
BR
Hy guys,

Let me ask you to have a little of your big knowledge about VB: I'm writing to a text file in VB. But I noticed that as usually in Windows programs, every line has a "^M" character at the end. A Unix program will use the file, and they don't like "^M".

Is there a way not to write "^M" when using VB to write files?

Thanks!

And happy independence day!
 
Use the Replace function. ^M is a Carriage Return, and most Windows and DOS programs use ^M and ^J (Carriage return and LineFeed) to terminate a line.
Unix normally expects just ^J.

Just use :
myUnixString = Replace(myVBString, vbCRLF, vbLF)




________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Hi,

you can avoid this by writing the file with the "Put" command after a Binary open:

hf%=FreeFile
Open sFileName$ For Binary Access Read Write Shared As #hf%
For i% = 1 to 10
s$ = "Test string " & cstr(i%)
Put #hf%, , s$
Next i%
Close hf%

in this example you won't have any "lines", you won't have any CrLf at all.....
 
LuckyStarr

Did either of those answers work? I notice you haven't come back on any of the questions you've asked to say if they worked or not!


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
i am also interested in the answer...

Known is handfull, Unknown is worldfull
 
If you are using Print or Write just add a semicolon (;) at the end of each line to suppress the Cr/LF.
 
Hi, sorry. I was offline this weekend. I'll try all this tips and tell you what happened.

Thanks, I though I would have to do it all over again using C, with a DLL!
 
The Replace function didn't work: the string doesn't have CRLF neither LF to be replaced!

I think they are written because of the Write function...

Take a look at my code: It's very simple. I have to write a number in a file (only this!), but I don't want "^M" after it.

-------------------------------------
filename = report_path + filename

Open filename For Output As #relat_file
Write #relat_file, unix_fct_id
Close #relat_file
------------------------------------

By the way... which function converts Long to String?

PS: I don't know if the binary solution will work, Will the string be written in human readable format?

PS2: I didn't understand the semicolon (;) solution. Put it where? What line?

Thanks!
 
The string as you write it doesn't have CRLF - it's automatically appended by the Write # statement. You need to remove it when you read the file. As I said before, if you're reading with Unix it only expects a LF (^J)(CHR(10)) as a delimiter.


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
You might try changing the line
Write #relat_file, unix_fct_id
to
Print #relat_file, unix_fct_id; vbLf;



Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
If you use Print with the semicolon then no delimiter is appended - so you get one continuous string. It's a hangover from the old Basic/BasicA/GWBasic etc. It won't help you in this situation as it's the Write # statement that's adding the CRLF.


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Thanks guys, both the binary write solution and the "Print #relat_file, unix_fct_id; vbLf;" solution worked!

Thanks a lot.
 
I'll just add that the Binary solution is much faster than the Print since you don't do any String manipulation...
 
Have you run controlled timing tests against the two approaches. I'm curious to know how much faster it is.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
:) :) :) :)

thanks for asking the test because it took the same time!

My tests where to write 100 000 times a string of 420 bytes (making a 40Mb file) and running the test several times shows the same time with a 0.1 second variation.

Sorry for my precedent assumption :) next time I'll test before speaking.....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top