?
Solved

String Behaviour.  Unusual Characters??

Posted on 2003-11-26
7
Medium Priority
?
522 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
[X]
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
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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