Click to See Complete Forum and Search --> : Advance file writing


Cakkie
February 2nd, 2000, 07:54 AM
I have this problem, i'm writing an app that transfers files across a network. This is done in packages from 512 bytes. These are read from a file, and sent trough a winsock control. At the other end, these must be written to a file. The problem is when writing it to a file, is is inserted on a new line.

eg:
myfile.dat contains following data:

this is just an example file
no need to pay very much attention to it

When i read some data, like say the first 10 bytes, this would result in "this is ju", when i write this to a file, there is no problem yet, but when the second package arives, containing "st an exam", it must be appended to the first line, not on a new line.

What i get is this file

this is ju
st an exam

what is should be

this is just an exam

Chris Eastwood
February 2nd, 2000, 08:07 AM
What are code are you using to write to the file ?

How are you opening the file for output (ie. Binary / Normal) ?

The below code shows a method similar to what you want - I'm writing away a byte array (created from a string) to a file. As you can see from the code it mixes normal text with 'vbCrLf' characters :


Dim i as Integer
Dim bByt() as Byte
Dim l as Long
Dim sVal as string
'
i = FreeFile
'
Open "c:\test.dat" for binary Access Write as i
'
sVal = "This is Line1" & vbCrLf & "this is line 2"
'
for l = 1 to len(sVal) step 10
' test to see what's being output
Debug.print mid$(sVal, l, 10)
'
bByt = mid$(sVal, l, 10)
' write it away to the file
Put #i, , bByt
next
'
Close #i




The output file, test.dat, should then contain :

This is Line1
this is line2



Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb

Spectre
February 2nd, 2000, 08:31 AM
If you use Print #FileNumber, "What you want to write to the file"
to write to a file - VB appends a Chr$(13) (End of line) to what is being written to the file. This is why if you write using this command - then write again to the same file with this command - 2 lines are written. If you add a ; to the end of the line - no Chr$(13) is added. Example

Print #FileNumber, "This is a ";
Print #FileNumber, "complete sentence."
will produce the following line in the file.
This is a complete sentence.