reversing a text file line by line - problem
Posted: Tue Feb 24, 2004 1:51 am
Hello all! I am a new user of PureBasic and it looks to be a fantastic tool! I was converting an older project over and was implementing a procedure that would read a file into an array of text lines and write them out in reverse order. The procedure takes two string inputs representing the filenames. It was working fine so it seems, except when I had the input and output file names the same ( I do expect that there would be many times when the user would prefer to overwrite the original file). When the file names are the same the debugger reports that there is no current file on the line in the code that attempts to write. I suspected that I needed to close the original file first, but no luck there. At this point, I don't know if I am battling a windows file system block (this is Windows XP home) or if I just am missing something obvious about how the PureBasic commands work... Below is the code in current form after sullying it with lots of debug or similar messages in a try and track down what was happening....I tried to delete the original file after its lines were read in, also to no avail (it's still in the directory)... Any help would be much appreciated! I suspect there is probably a more elegant way to do this than this method, but the line array is useful for several other procedures that manipulate the lines as well... Here is the procedure I am battling with: In a nutshell I don't understand why my result is 0 to open a file with the same filename I have read in....?
Procedure reverseFile(inputFile.s,outputFile.s)
result= ReadFile(0,inputFile)
If result=0
MessageRequester("File Missing", "You need " + inputFile + " in the folder", 0)
Else
;First we need to figure the number of lines
i=0
Repeat
;First$ = Trim(ReadString())
First$ = ReadString()
i=i+1
Until Eof(0)
;MessageRequester("PureBasic", "Lines read: "+Str(i), 0)
Dim lineArray.s(i)
result= ReadFile(0,inputFile);"script.txt")
For j=1 To i
lineArray(j)=ReadString()
;Debug lineArray(j)
Next j
;CloseFile(0)
Debug "inputfile is " + inputFile
If inputFile=outputFile
Debug "Deleting " + inputFile
DeleteFile(inputFile)
EndIf
Debug "output filename is " + outputFile
result=OpenFile(3,outputFile)
Debug "Result is " + Str(result)
For j=i To 1 Step -1
WriteStringN(lineArray(j))
Next j
;Debug "Lines read was: " + Str(j-1)
EndIf
EndProcedure
Procedure reverseFile(inputFile.s,outputFile.s)
result= ReadFile(0,inputFile)
If result=0
MessageRequester("File Missing", "You need " + inputFile + " in the folder", 0)
Else
;First we need to figure the number of lines
i=0
Repeat
;First$ = Trim(ReadString())
First$ = ReadString()
i=i+1
Until Eof(0)
;MessageRequester("PureBasic", "Lines read: "+Str(i), 0)
Dim lineArray.s(i)
result= ReadFile(0,inputFile);"script.txt")
For j=1 To i
lineArray(j)=ReadString()
;Debug lineArray(j)
Next j
;CloseFile(0)
Debug "inputfile is " + inputFile
If inputFile=outputFile
Debug "Deleting " + inputFile
DeleteFile(inputFile)
EndIf
Debug "output filename is " + outputFile
result=OpenFile(3,outputFile)
Debug "Result is " + Str(result)
For j=i To 1 Step -1
WriteStringN(lineArray(j))
Next j
;Debug "Lines read was: " + Str(j-1)
EndIf
EndProcedure