Community Forum

add line in file

Forum Overview >> Scripting

CategoryScripting
Created20.05.2015 22:14


Fred Zanella (fredzaza2) 20.05.2015 22:16
hi , i want to add a line in a file without erasing the first part of this file.When i use " io.open (filename, "w")" the first part is deleted . it is posible to add line in back or middle of a file ?

Emil Drefers (Unknown) 21.05.2015 08:00
Hi,

no, you can only write to a file by erasing its content first.
But you could try to read the content of the file before writing to it ;)

Cheers,
Emil

Fred Zanella (fredzaza2) 21.05.2015 19:08
so , i can use a function for reading the first part , save and rewrite my new line and the first part ?

Emil Drefers (Unknown) 22.05.2015 07:27
Hi,

by using
local file = io.open(filename, "r");
you should be able to open a file for rading its content.

What you do with the information in the file afterwards is up to you ;)

Cheers,
Emil

Fred Zanella (fredzaza2) 22.05.2015 17:26
ok , thank you for your help

Karl Schneeberger (Unknown) 26.07.2015 20:47
Hi,

did that work?
If i try to do that, I get always a warning "Warning: io.open, only write mode ('w') is allowed"
and the file is not opened.

Part of my code:

local oldFile;
if fileExists(newFileName) and fileExists(oldFileName) then
oldFile = io.open(oldFileName, "r");
.....
end;

What's wrong with my code?

Regards
Karl


Emil Drefers (Unknown) 27.07.2015 07:55
Hi,

that's correct io.open only works in the "w" mode.

But there's an other way to read the contents of an xml file.

e.g. line 14 in:
http://ls-mods.de/scriptDocumentation.php?lua_file=handTools/Chainsaw

Cheers,
Emil

Karl Schneeberger (Unknown) 27.07.2015 13:53
Hi,

you mean line 24?
??local xmlFile = loadXMLFile("TempXML", xmlFilename); ??

My problem is, that i would like to access "inputbindings.xml"
Unfortunately, all the entries have the same secondLevel.id (input or axis).

So, i can not access a specific line by the ..XML.. functions.

Regards Karl

Emil Drefers (Unknown) 27.07.2015 14:40
Hi,

ups, yes I meant line 24.

Well, you could search for the desired line by checking the "name" attribute. (?)


Cheers,
Emil


Karl Schneeberger (Unknown) 27.07.2015 15:22
Hi,

i tried the following:
.....
xmlFile = loadXMLFile("input_XML", fileName, "gamePadOrder");
......
local res = getXMLString(id, "inputBinding.input#name");
....

Yes, that works for the first entry.
But how do i get all the others? They all start with the same XMLPath...
Karl


Emil Drefers (Unknown) 28.07.2015 08:12
Hi,

well, you could have had a look at the scripts in the script documentation to understand how you can access such elements.
http://ls-mods.de/scriptDocumentation.php

Anyhow ...

first element: getXMLString(id, "inputBinding.input(0)#name");
second element: getXMLString(id, "inputBinding.input(1)#name");
third element: getXMLString(id, "inputBinding.input(2)#name");
so on and so forth ...

in a loop you could do something like:
getXMLString(id, string.format("inputBinding.input(%d)#name", i));

Cheers,
Emil

Karl Schneeberger (Unknown) 28.07.2015 16:24
Hi,

that was the needed information to solve my problem.
Thanks a lot, you made my day.

Suggestion:
- Collect all the scripting information on one page.
The info about XML access is on this site and not under "ls-mods.de".
- Add the information about ..input(x)#name... to the documentation on this site under the Chapter XMLPath.
- Fix the missing or bad links in the documentation on this site.
- index to XML
- several Links to XMLPath

Regards
Karl


Note: Log in to post. Create a new account here.