Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

String Behaviour.  Unusual Characters??

Posted on 2003-11-26
Last Modified: 2010-05-01
I have a text file that contains a large string of characters,  alot of these chars are unusual ones such as Null characters etc etc.  I read this file into a string in my program using "Line Input #file, strMyString" - once I have read the file into the string I then print it to a new file using "Print #newfile, strMyString".  

The process appears to work nicely,  until I compare the original file with the new file,  it would seem that somewhere in the process VB has dropped or converted or done something to some of the more unusual chars,  so comparing the original file with the new file - they are quite different.  This creates a big problem.

Are there some chars that VB cant put into a string?  Why is this happening?

Thanks in advance.

Question by:eclipse707
  • 4
  • 2

Expert Comment

ID: 9827374
Do you need to read it into visual basic first?

Can you not just copy the file:

FileCopy source, destination

hope this helps,

LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9827478
From the VB Help File:

"The Line Input # statement reads from a file one character at a time until it encounters a carriage return (Chr(13)) or carriage return–linefeed (Chr(13) + Chr(10)) sequence. Carriage return–linefeed sequences are skipped rather than appended to the character string."

So you can't use "Line Input #x" to preserve the file contents since it strips things out.  Instead, you can use the Get() and Put() functions in Binary File Access Mode to read the file into a byte array.

    Dim binaryData() As Byte
    Open "File1" For Binary Access Read As #1
    ReDim binaryData(LOF(1))
    Get #1, , binaryData
    Close #1
    Open "File2" For Binary Access Write As #2
    Put #2, , binaryData
    Close #2

This reads "File1" in all at once and then writes it back out to "File2".  You could always read the file in smaller chunks if you needed to.  This is the same method used for reading/writing binary files with TCP/IP or FTP (where you would definitely use smaller byte arrays).


Author Comment

ID: 9827627
That would be nice,  but I must make some changes to the file :-(

Works great,  thanks,  just what I wanted....almost....

What I neglected to say in the original post was that once I have read the original text file into my program I do some work with the string,  basically, the text file contains some empty fields which I must populate.  To do this,  I must have a string to look at and work with (I guess?).

Using your method above to read/write to the files,  how can I in between the reading and writing of the text file,  perform the processing of the string?

Thanks again.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9827965
Try reading the file this way then:

Dim entireFile As String
Open "File1" For Binary Access Read As #1
entireFile = Input(1, LOF(1))
Close #1

LVL 85

Accepted Solution

Mike Tomlinson earned 200 total points
ID: 9828005

got the syntax backwards.  Should be:

Dim entireFile As String
Open App.Path & "\File1.txt" For Binary Access Read As #1
entireFile = Input(LOF(1), 1)
Close #1


Author Comment

ID: 9828126
Thanks mate,  just what I needed.
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9828144
My Pleasure.


Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
using Access 8 75
VB6 - Convert HH:MM into Decimal 8 62
clicking a shape in a frame array vb6 3 46
VB6 ListBox Question 4 48
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question