Author Topic: File Reading/Writing  (Read 1393 times)

Vortrex

  • Full Member
  • ***
  • Posts: 267
  • Karma: +54/-73
    • View Profile
File Reading/Writing
« on: February 16, 2013, 06:25:16 am »
I have a quick question about file reading/writing. I cannot seem to figure out how to take a normal file and outputting its contents and other manipulations that I may need.

I downloaded a GeoLite database with some GeoIP information. It's in the form of a CSV (Comma Seperated Value) file. I know how to parse this using Squirrel, so I don't need any help there.

My question is, how do I output the file to a variable or something, so I can further process it? It's in the form of an instance.

I hope its not something that's right under my nose, but I'm assuming I'm just missing something simple.

Thanks in advance,
Vortrex

Nihau

  • Newbie
  • *
  • Posts: 49
  • Karma: +27/-18
    • View Profile
Re: File Reading/Writing
« Reply #1 on: February 16, 2013, 09:28:13 am »
Code: [Select]
function FileWrite(filename, text )
{
local file_text, file_handle;
file_text = text + "\r\n";
file_handle = file( filename, "a+" );

foreach ( idx, chr in text ){
file_handle.writen( chr, 's' ); }
}

Code: [Select]
FileWrite("Logs/Stats.txt", "hello");
Btw its edited Windlord `s script.
« Last Edit: February 18, 2013, 05:07:30 pm by Vetal »

Nihau

  • Newbie
  • *
  • Posts: 49
  • Karma: +27/-18
    • View Profile
Re: File Reading/Writing
« Reply #2 on: February 18, 2013, 06:03:12 pm »
http://forum.iv-multiplayer.com/index.php?topic=5911.0
Code: [Select]
function FileRead(filename)
{
    local myText = "", file_handle = file(filename, "r+");
    while(!file_handle.eos()) {
          myText += file_handle.readn('s').tochar();  }
    return myText;
}

But this code cant read every file (file encoding should be UCS-2 LE w/o BOM).

In such situations, playing with  readn type  might help.

 

© Liberty Unleashed Team.