Solved

String Behaviour.  Unusual Characters??

Posted on 2003-11-26
7
516 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 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).

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Idle_Mind
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 200 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 85

Expert Comment

by:Mike Tomlinson
ID: 9828144
My Pleasure.

Idle_Mind
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now