Open File in a Directory
BlitzMax Forums/BlitzMax Beginners Area/Open File in a Directory
| ||
Hi all, I have a function which saves a file using RequestFile, CreateFile, OpenFile, and CloseFile. The file is created correctly, and in the right place with RequestFile and CreateFile, but when I open the file with OpenFile it returns null. Here's the code: Function saveFileAs() Local time:String = CurrentTime$() Local timeOut:String[] = time.split(":") Local timeString$ = timeout[0]+timeout[1]+timeout[2] filter$="Character Files (.chr);All Files:*" filename$=RequestFile( "Save File As:",filter$,True) saveFile=CreateFile(filename$) saveLns=OpenFile(filename$) WriteLine saveLns,"RPG-X Character Creator Saved Character File" WriteLine saveLns,"Created on "+CurrentDate$()+" at "+timeString$+"." WriteLine saveLns,"-------------------------------------------------" WriteLine saveLns,"" WriteLine saveLns,"Name: "+TextFieldText(characterName) WriteLine saveLns,"Age: "+TextFieldText(characterAge) WriteLine saveLns,"Gender: "+SelectedGadgetItem(charGenderComboBox) CloseFile savedLns Return 0 End Function All help is appreciated - thanks! |
| ||
Don't use OpenFile. Use WriteFile and ReadFile instead. |
| ||
I tried that... no luck. Same thing happens. |
| ||
Don't know then. If ReadFile is returning Null, then I'd be 99.99% certain that you've got the file/path wrong. You should post some more code (like, the code where you're trying to read the file - i.e. where the error is). |
| ||
I've fixed it. There was a very simple typo, that's all. Thank you. |
| ||
Well, just so you're aware - if by any chance a file happens to be read-only, it won't open with OpenFile. ;) |
| ||
I knows that. I have another question. I have a biiig file, a config file for something, and I want to edit a certain line which begins with, say, "setup character bind "0"" how do I find that line so I can edit it? |
| ||
Open the file. Read all lines in a list or array. Check all lines: local line:string If line.contains("setup character bind ") ' replace line with new one end if I dont know about searching for " in a string; may cause problems! Then write the list/array back to file. There is no way to edit something in the middle of a file as far as I know. |
| ||
I dont know about searching for " in a string; may cause problems! local line:string If line.contains("setup character bind ~q0~q") ' replace line with new one end if |
| ||
Hmm... so I would store each line of the file in an array, and read each line until I find what I want? That would work, but I'm a bit shaky on how I'd actually go about copying the file into an array, then writing it back out into the file... This help is great; it's very much appreciated :) |
| ||
I prefer a TList for this:SuperStrict Local filetoread:TStream = OpenFile("test.txt") ' make sure that there is a file with this name! Local filelist:TList = New TList ' Read from file While Not Eof(filetoread) ' reading the file to the list Local line:String = ReadLine(filetoread) ' read a line filelist.AddLast(line) ' add it to the list Wend CloseFile(filetoread) ' write list back to file Local filetowrite:TStream = OpenFile("test.txt") For Local line:String = EachIn filelist ' cycle throug the list WriteLine (filetowrite, line) ' write line to file Next CloseFile(filetowrite) ' Better never forget, Windows opens the same file only a few hundred times :-) |
| ||
Alright, I'll try adding the replace code in there as well :) Something like this: Local filetoread:TStream = OpenFile("test.txt") ' make sure that there is a file with this name! Local filelist:TList = New TList ' Read from file While Not Eof(filetoread) ' reading the file to the list Local line:String = ReadLine(filetoread) ' read a line filelist.AddLast(line) ' add it to the list If filelist.line.contains("REPLACE") filelist.line.contains = "replaced" end if Wend CloseFile(filetoread) ' write list back to file Local filetowrite:TStream = OpenFile("test.txt") For Local line:String = EachIn filelist ' cycle throug the list WriteLine (filetowrite, line) ' write line to file Next CloseFile(filetowrite) ' Better never forget, Windows opens the same file only a few hundred times :-) |
| ||
That will not work. You misuse the string method 'contains'. Just assing a new value to the readed line before storing it in the list: SuperStrict Local filetoread:TStream = OpenFile("test.txt") ' make sure that there is a file with this name! Local filelist:TList = New TList ' Read from file While Not Eof(filetoread) ' reading the file to the list Local line:String = ReadLine(filetoread) ' read a line ' If line.Contains("REPLACE") ' check for searched string line = ("replaced") ' replace readed line with the new line End If ' filelist.AddLast(line) ' add it to the list Wend CloseFile(filetoread) ' write list back to file Local filetowrite:TStream = OpenFile("test.txt") For Local line:String = EachIn filelist ' cycle throug the list WriteLine (filetowrite, line) ' write line to file Next CloseFile(filetowrite) ' Better never forget, Windows opens the same file only a few hundred times :-) |