• Lua file I/O (Not in Gmod)
    22 replies, posted
I was wondering, does anyone know off the top of their head how to do I/O on files in Lua? Specifically, I can't seem to find a way to replace data in a file, rather than just appending to it. Anyone know how it's done, or just wish to comment on the subject? I thank you.
[lua] local f = io.open("yourfile.txt", "r") -- "r" means read-mode local text = f:read("*a") -- read whole file f:close() -- close file handle local newtext = text:gsub("*replace me*", "*replaced*") --replace all instances of *replace me* with *replaced* f = io.open("yourfile.txt", "w+") -- "w+" means "write in update mode" (erases previous contents) f:write(newtext) --write the new data f:close() --close file handle [/lua]
Aaah, it had to do with those strange little letter/symbol pair arguments. Thank you kindly, especially for such a swift response.
The io library and the file functions are documented here: [url]http://www.lua.org/manual/5.1/manual.html#5.7[/url]
Damn it cop, how many languages do you know?
[QUOTE=DarkSpirit05er;17036542]Damn it cop, how many languages do you know?[/QUOTE] Plenty :love:
[QUOTE=DarkSpirit05er;17036542]Damn it cop, how many languages do you know?[/QUOTE] once you learn programming, languages are pretty easy to pick up.
your avatar is sooo true
[QUOTE=DarkSpirit05er;17036542]Damn it cop, how many languages do you know?[/QUOTE] he knows a lot, most of which are [url=http://en.wikipedia.org/wiki/D_(programming_language)]worthless[/url].
Worthless? D seems pretty interesting so far, but I'd like to know how efficient the compilers are.. It seems like very decent variant of C++.
[QUOTE=ZeekyHBomb;17044353]Worthless? D seems pretty interesting so far, but I'd like to know how efficient the compilers are.. It seems like very decent variant of C++.[/QUOTE] On my system a hello world program in D compiles to a 206kb executable :geno:
So does a C++ one if you use iostream.
[QUOTE=ZeekyHBomb;17044353]Worthless? D seems pretty interesting so far, but I'd like to know how efficient the compilers are.. It seems like very decent variant of C++.[/QUOTE] There's pretty much DMD, GDC (front-end for GCC) and LDC (front-end for LLVM) to choose from. LDC seems to produce the most efficient code. Although LDC is progressing fast, DMD still supports the most of the language. GDC is getting very outdated, and nobody is really working on it anymore, so it's not really recommended. [QUOTE=Kat of Night;17044432]On my system a hello world program in D compiles to a 206kb executable :geno:[/QUOTE] With Tango, it starts at about 150kb because of the runtime. It holds the GC, all libraries from the standard library you actually use, and lots of type info. Imagine if an executable made in C# had to include everything it used from .NET, including the runtime itself.
[QUOTE=jA_cOp;17044491]There's pretty much DMD, GDC (front-end for GCC) and LDC (front-end for LLVM) to choose from. LDC seems to produce the most efficient code. Although LDC is progressing fast, DMD still supports the most of the language. GDC is getting very outdated, and nobody is really working on it anymore, so it's not really recommended. With Tango, it starts at about 150kb because of the runtime. It holds the GC, all libraries from the standard library you actually use, and lots of type info. Imagine if an executable made in C# had to include everything it used from .NET, including the runtime itself.[/QUOTE] I was using DMD
[QUOTE=Kat of Night;17044543]I was using DMD[/QUOTE] You can use Tango with DMD, too. [url]http://www.dsource.org/projects/tango/wiki/DmdDownloads[/url]
I'm more interested in speed optimizations. C/C++ is still widely used and as such you can expect the compilers to be producing very fast assembly (depending on your code, the compiler and linker-settings of course). I couldn't find a speed comparison chart/benchmark for D vs C/C++ per Google though.
[QUOTE=ZeekyHBomb;17044731]I'm more interested in speed optimizations. C/C++ is still widely used and as such you can expect the compilers to be producing very fast assembly (depending on your code, the compiler and linker-settings of course). I couldn't find a speed comparison chart/benchmark for D vs C/C++ per Google though.[/QUOTE] Here, I did a couple tests with the simplest Hello World program, using the unix program time: D: [code]hello world real 0m0.029s user 0m0.001s sys 0m0.002s [/code] C: [code]hello world real 0m0.002s user 0m0.000s sys 0m0.002s [/code] Second run: D: [code]hello world real 0m0.003s user 0m0.001s sys 0m0.002s [/code] C: [code]hello world real 0m0.002s user 0m0.000s sys 0m0.002s [/code] They were both compiled with no options beyond specifying an output file for hello.c. It seems like C was consistent, with the same time every time, whereas D had a relatively slow first time before getting significantly faster - but still not quite as fast as C. All subsequent runs produced identical results to the second run.
Why would you benchmark a Hello World program :sigh: D runs static module constructors and initializes the GC (by default) upon startup. C has neither of those features. If you want to make sure no module constructors are run, simply use the C standard library instead: [cpp] extern(C) int printf(char* format, ...); int main() { printf("Hello, world!\n".ptr); return 0; } [/cpp]
I'd be interested in more sophisticated algorithms and a compile with full optimizations. Maybe I should just get my feet wet with D to do my own comparisons. Do you have the option not to use the GC and solely relay on reference counting pointers an such - or complete manual memory management?
[QUOTE=jA_cOp;17047147]Why would you benchmark a Hello World program :sigh: [/QUOTE] Why not? I was curious and thought it would be interesting.
[QUOTE=Sporbie;17044484]So does a C++ one if you use iostream.[/QUOTE] [img]http://i28.tinypic.com/24qn8ch.png[/img] the fuck are you on about
[QUOTE=Kat of Night;17047209]thought it would be interesting.[/QUOTE] Just what does a Hello World program demonstrate? [QUOTE=ZeekyHBomb;17047185]I'd be interested in more sophisticated algorithms and a compile with full optimizations.[/QUOTE] I could only find one, demonstrating Tango's XML library: [url]http://dotnot.org/blog/archives/2008/02/[/url] [QUOTE=ZeekyHBomb;17047185]Do you have the option not to use the GC and solely relay on reference counting pointers an such - or complete manual memory management?[/QUOTE] You do, but you'd have to find a runtime/standard library that doesn't use one (or make your own). I know there are a few, but I haven't been interested myself. [QUOTE=efeX;17047264][img]http://i28.tinypic.com/24qn8ch.png[/img] the fuck are you on about[/QUOTE] He was probably compiling with debug symbols enabled.
-snip-
Sorry, you need to Log In to post a reply to this thread.