Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading in everything from a text file 2

Status
Not open for further replies.

Jooky68

Programmer
Jul 10, 2002
231
US
I have been trying to do encryption using the api. After I am encrypting the data I am saving it to a text file which works fine. While doing the encryption I tried to make sure that it didnt not incorporate a cf or lf character. But sometimes I am still having trouble reading in everything from the text file (that is encrypted). The file contains this following string exactly:

´o¸sçPQ]××¢ÎåhøoÔJ2¢1´%Áüø*RM@-Æ

When I attempt to read in a line, or use the input() function with the buffer the size of the file it will never read beyond the 8th charater. If I attempt to read in 9 bytes from the file, I get an input past the end of the file error.

Does anyone know of a method where I can read everything in? Or maybe a possible work around for this?
 
Try the FileSystemObject:

Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso_OpenTextFile("c:\somefile.txt", ForReading)
ReadAllTextFile = f.ReadAll
MsgBox ReadAllTextFile

"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 
You have taken care of Cr and Lf characters but what did you do for Null characters. From the sample encrypted string it seems that it is binary data and most likely to contain null characters as well(?)

If they appear in the file you'll not succeed in reading the data in text mode. Rather you should read the file using binary I/O mode, or write your encryption code so that it does not produce a null character in the encrypted string.

When reading the file in text mode, an 'Input past the end of the file' error will be signaled whenever a null character is encountered and no further data will be read.
 
After I do the encryption I go into a loop and keep looping till none of the unwanted characters are in the encryption.

Do While (InStr(1, sEncrypted, vbCr) > 0) _
Or (InStr(1, sEncrypted, vbLf) > 0) _
Or (InStr(1, sEncrypted, Chr$(0)) > 0) _
Or (InStr(1, sEncrypted, vbTab) > 0)

'in here i would do the encryption again

loop


That should filter out the CR,LF, NULL and tab. I have no idea why I cant read in from the file though some of the time. Anyone?
 
DrJavaJoe:

I have tried the readall method with the fso, and it does work. But it seems to add 2 characters to the end of string that is read in from the text stream. I am not really sure what these two characters are, do you know? I am guessing that it is a CFLF combination??

If that is the case I would continue to just use that method. Also has anyone encountered any serious problems with using the FSO Object Model?

Thanks for the responses.

Paul J.
 
That is the case, the characters would be Chr(13) and chr(10).

"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 
Just treat it all as a block of binary - come away from the notion of characters...

 
I tried using the binary method and it writes to the file fine. But I am having trouble reading from the file. It doesnt read anything in. Any clue as to why it would do this? Here is the coding that I use. encrypted_data is string(not sure if that is the problem).

f = FreeFile 'get file number
Open destfile For Binary As #f
Get #1, , encrypted_data
Close #1
 
Try:

Private Sub Command1_Click()
Dim destfile As String
Dim encrypted_data As String
destfile = "C:\Users.txt"
f = FreeFile 'get file number
Open destfile For Binary As #f
encrypted_data = Space(FileLen(destfile))
Get #1, , encrypted_data
MsgBox encrypted_data
Close #1
End Sub

Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top