file handling - how to question.
Blitz3D Forums/Blitz3D Beginners Area/file handling - how to question.
| ||
I never did file handling things, I wonder how can I read a txt file, search for the line where it starts by gamma which is line: 15 line theres a : gamma = 0.5 I want to read it, and be able to write on that line cause i wana change the gamma value, how would u do it? |
| ||
Writing on a specific line is complicated because all data in the file is sequential. This means that the following line immediately follows the 0.5 (in your example) part of the file. You could edit the file by using OpenFile and then SeekFile'ing to the desired byte, WriteByte'ing the desired byte(s), and then Closefile. Unfortunately if the number of bytes you write are shorter or longer than the bytes already there, you will mess up the file. What I am getting at is that if you were to replace 0.5 with 0.8 for example, then there would be no problem. BUT, if you were to replace 0.5 with .5 or 0.83 then funny things would happen. .5 would actually become .55 and 0.83 would become 0.83 but the three would write over the first character of the next line which would make your gamma= statement equal everything on the following line as well. In this example < will represent line feed character previousline<gamma=0.5<followingline ;changed to 0.8 would be previousline<gamma=0.8<followingline ;just what you want previousline<gamma=0.5<followingline ;changed to .5 would be previousline<gamma=.55<followingline ;extra 5 wasn't erased previousline<gamma=0.5<followingline ;changed to 0.83 would be previousline<gamma=0.83followingline ;line feed character erased so now gamma = 0.83followingline So what you would need to do if you want line by line editability is use the SeekFile and FilePos commands to: -find the spot that requires changing by parsing through the file until you find the phrase your are looking for (such as "gamma") -note the FilePos where you found the phrase -store everything before that spot (FilePos) in a string variable (say Beginning$) -store everything after that spot in another string variable (say Remainder$) -overwrite the entire file with: Beginning$ + "0.83" + Remainder$ ***Alternative method*** Another method you could implement would be to structure your file in such a way that you don't use variable length lines. Every line in your file is exactly the same length so you can easily change any line and know that you won't overwrite anything else. You could have a structure something like this: variablename = floatingpointvalue AAAAAAAAAAAA=XXXXXXX.XXXXXX gamma = 0.5 volume = 75 zoffsetvalue= 45903.239594 xoffsetvalue=1003478.002This way if you wanted to change any given value (say the volume percent to 80), you could just skip to the second 'line' by SeekFile'ing to (2 - 1) times 27(the linelength) plus 13(offset to value part of the line) and WriteByte'ing the characters " 80 "(exactly 14 characters long) (NOTE that you wouldn't actually write the quotes, they are just there for clarity) The downfall to this method is that if a user were to modify the file and change the structure in any way, it would become corrupted. So, the safest bet is to use the first method. The easier way is to use the second method. Note that these two methods are not the only ways either. |
| ||
How would I do it just read line 15 gamma = 0.5 store 0.5 in gammaNum and just change the value in the program and when he press save, it just rewrite line 15 with: gamma = whatever gammaNum as in it How would I do that, file handling seems very weird :| remember i know nothing about filehandling .. |
| ||
I just told you. |
| ||
I dont get explane urself better!!!!! and its not my file its a file of another thing that i just cant change. i only can change the values |
| ||
Sounds like you need to write a scriptparser ... have fun :) |
| ||
Reread my first post. Try to understand it. If the file you refer to is not your file, then how can you possibly know what to expect in it? I'm sorry but I don't understand your situation. Try to explain YOURSELF better. As Jerry McGuire said: Help me help you. |
| ||
Its a game file in another folder, a game that I didn't create if I try to modify it your second way, the game won't work. The program I'm trying to do is just to edit certain lines. the gamma line is the 15 line, I need to read it, put whatever value it has after the "gamma =" which in this case is 0.5, store it into gammaNum Global variable. Display on my screen a "Gamme:" and "TextBox with the value of gammaNum, which is editable" and when he press save i tjust write it to the file. Is this really hard to do? Isn't there a simple command to get line 15 at possition 50(for example)? |
| ||
You can't insert data into the middle of a file. You have to read the entire file and write a new modified file. Const MAXLINES 200 Dim dat$(MAXLINES) ; read file f=ReadFile("gamefile.txt") linecount=0 While Not Eof(f) linecount=linecount+1 dat$(linecount)=ReadLine(f) If linecount=MAXLINES Exit Wend CloseFile f ;modify file dat$(15)="i will be banned if i swear on blitzbasic.com forums" ; write file f=WriteFile("gamefile.txt") For i=1 To linecount WriteLine f,dat$(i) Next CloseFile f |
| ||
Wow seems exactly what I wanted, however I didn't test yet :) |
| ||
You can't insert data into the middle of a file. You have to read the entire file and write a new modified file. This is exactly what I said. There can be a problem with the code skidracer posted. You are making the assumption that line 15 will always be the gamma= line. If it's not, you will overwrite some other (possibly important) line. You really should check to make sure that the line is the one you want unless you know for a fact that it will always be on line 15. You can parse through each line and check for the phrase segment "gamma" (preferably using the Lower$ or Upper$ commands). Then modify THAT line. |
| ||
Thanks WolRon ;) However I know for a fact that its always 15 in this case :) However I wouldnt know how to search for gamma in the file soo.... |
| ||
I've done an INI file function in the code archives if you're interested... I think it does exactly what you want. http://www.blitzbasic.com/codearcs/codearcs.php?code=776 |