Jump to content
Sign in to follow this  
stev379

FileRead, close then write

Recommended Posts

stev379

Is is possible to FileRead File"A", write some of the data to File"B", then close both when the end of File"A" is reached, then open File"B" and Read from it?

I can open the first two files and read and write from them respectively, I then FileClose both, but when I try to open File"B" for reading, it's erroring with "-1".

Share this post


Link to post
Share on other sites
herewasplato

Is is possible to FileRead File"A", write some of the data to File"B", then close both when the end of File"A" is reached, then open File"B" and Read from it?

I can open the first two files and read and write from them respectively, I then FileClose both, but when I try to open File"B" for reading, it's erroring with "-1".

It might have been better if you had posted your code or a sample that reproduces your issue.

C:\Temp\testFileA.txt contains this text:

text from file a

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Is is possible to FileRead File"A",
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
$fileA = FileOpen("c:\temp\testFileA.txt", 0)
$fileB = FileOpen("c:\temp\testFileB.txt", 2)

If $fileA = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

If $fileB = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in 1 character at a time until the EOF is reached
While 1
    $chars = FileRead($fileA, 1)
    If @error = -1 Then ExitLoop

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; write some of the data to File"B", 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    $ans = MsgBox(4, "Char read from fileA:", _
    $chars & @CRLF & "Want to write this to fileB?")
    If $ans = 6 Then FileWrite($fileB, $chars)
Wend

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;then close both when the end of File"A" is reached
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FileClose($fileA)
FileClose($fileB)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;then open File"B" 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
$fileB = FileOpen("c:\temp\testFileB.txt", 0)

If $fileB = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;and Read from it?
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
MsgBox(0,"FileB",FileRead($fileB, FileGetSize("c:\temp\testFileB.txt")))
FileClose($fileB)
This works for me.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.