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
reversing a text file line by line - problem
Hi there, I think ive solved your problem, you didnt close the file after opening it the first time. Heres the new code:
Ive indented it, added some comments and removed (hopefully) redundant debug stuff.
Hope this helps!!!
James
Ive indented it, added some comments and removed (hopefully) redundant debug stuff.
Code: Select all
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)
CloseFile ( 0 ) ;Make sure file is closed before we re-open it
;^^^^^ ADDED IN CHANGE ABOVE ^^^^^^
;*********Then we allocate space for them and read them from the file**********
Dim lineArray.s(i)
result= ReadFile(0,inputFile);"script.txt")
For j=1 To i
lineArray(j)=ReadString()
Next j
CloseFile(0) ;Make sure file is closed before we re-open it
If inputFile=outputFile
;ok we want to delete the old file
DeleteFile(inputFile)
EndIf
;*********And finally output it to the new file in reverse order**********
result=OpenFile(3,outputFile)
;loop from end of array to beggining
For j=i To 1 Step -1
WriteStringN(lineArray(j))
Next j
;Debug "Lines read was: " + Str(j-1)
CloseFile (3)
EndIf
EndProcedure
James
This community ROCKS! Thank you James!
I can't believe how quickly folks here reply to messages from a newbie, especially one with a big chunk of ugly code attached. Not only that, my cutting and pasting was an ugly job in that the indentions were lost and it was tough to read. Will do a better job by cleaning up in preview from now on...
James didn't only tell me what my problem was, he put up reindented cleaned up code that worked as posted!
Thank you again James! I hope to put in the time to be able to do the same for others in the near future. I really like what I see in this product and cannot believe it is the work of such a small team. It looks like the commercial team is engaged in a labor of love which has allowed a community of helpful code loving fiends to do what they love as well.
Best wishes all!
James didn't only tell me what my problem was, he put up reindented cleaned up code that worked as posted!
Thank you again James! I hope to put in the time to be able to do the same for others in the near future. I really like what I see in this product and cannot believe it is the work of such a small team. It looks like the commercial team is engaged in a labor of love which has allowed a community of helpful code loving fiends to do what they love as well.
Best wishes all!
When pasting code into your message wrap it inside a code block, like this:my cutting and pasting was an ugly job in that the indentions were lost
Code: Select all
[code]
MessageRequester("Example", "Hello World")
With all the formatting preserved.
You will get this:
Code: Select all
MessageRequester("Example", "Hello World")
thanks all!
Thanks for pointing out how to do that GedB!