Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


String Behaviour.  Unusual Characters??

Posted on 2003-11-26
Medium Priority
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
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
  • 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 86

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.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 86

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 86

Accepted Solution

Mike Tomlinson earned 800 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 86

Expert Comment

by:Mike Tomlinson
ID: 9828144
My Pleasure.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

636 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