Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

String Behaviour.  Unusual Characters??

Posted on 2003-11-26
7
Medium Priority
?
524 Views
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.

0
Comment
Question by:eclipse707
  • 4
  • 2
7 Comments
 
LVL 2

Expert Comment

by:robbrownuk
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,

robbrownuk
0
 
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).

Idle_Mind
0
 

Author Comment

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

Idle_Mind:
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.
0
Technology Partners: 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!

 
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

Idle_Mind
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 800 total points
ID: 9828005
Oops,

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

Idle_Mind
0
 

Author Comment

by:eclipse707
ID: 9828126
Thanks mate,  just what I needed.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9828144
My Pleasure.

Idle_Mind
0

Featured Post

Industry Leaders: 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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 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