CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Advance file writing

    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

    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  2. #2
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Advance file writing

    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

  3. #3
    Join Date
    Feb 2000
    Posts
    137

    Re: Advance file writing

    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.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured