Jump to content


Photo

Saving Text Files As Strings?


  • Please log in to reply
3 replies to this topic

#1 sh@de

sh@de

    GMC Member

  • New Member
  • 413 posts

Posted 17 January 2010 - 05:36 AM

Is there any way I can take a text file, and dump the whole thing into a string?

Thanks a lot in advance. :D

Edited by sh@de, 17 January 2010 - 05:37 AM.

  • 0

#2 peterc

peterc

    GMC Member

  • New Member
  • 40 posts

Posted 17 January 2010 - 05:56 AM

first create a script called 'text_to_string' and paste this:
[codebox]_loop = true
_filename = argument0
_contents = ""
_file = file_text_open_read(_filename)

for(_i = 0;_loop = true; _i+=1)
{
if!(file_text_eof(_file))
{
_contents = string(_contents) + file_text_read_string(_file) + string("#")
file_text_readln(_file)
}else
{
_loop = false
}
}

file_text_close(_file)
return _contents[/codebox]

then when ever you want to load a file into a string use this code:
[string] = text_to_string([filename])

**you have to replace the '[***]' with either a variable or filename
  • 0

#3 RocketLauncher3G

RocketLauncher3G

    Int. Game Programmer

  • GMC Member
  • 627 posts

Posted 17 January 2010 - 06:23 AM

without using a for loop:
[codebox]
//new script!
/*something like:
FileToString(file,NewLine (true||false));
argument0: file with path (working_directory temp_directory etc);
argument1: adding a # for a new line, true or false;
*/
var _file, _contents;
_file = argument0; //storing the file in a temp var, _file
if (argument1) { //If newLine is true, then add a #
_newLine = "#";
} else { //Else, Don't :P
_newline = "";
}
_contents = ""; //Contents = nothing (yet :P)
if (!file_exists(_file)) { //Checking if file exists, else you exit and return false :)
return false; //Like I said, returning an error :)
exit; //Exits this step, and this script
} else {
tmpFileName = file_text_open_read(_file); //storing the unread file contents in a variable
while (!file_text_eof(tmpFileName)) { //While the file hasn't ended..
_contents += file_text_read_string(tmpFileName) + string(newline); //add the string (and the newline if true) to the contents
file_text_readln(tmpFileName); //If you don't use this, you'll end up reading the first line an unlimited times :)
}
}
return _contents;
/*You return the contents so you could use this script like:
filecontents of file readme.txt:
--------------------------------------------------------------------------
Hello, this is my neat little example
of using

a text file to a string!
--------------------------------------------------------------------------
I'd like to read the contents of that text file: |
2 values, 2 outcomes :): |
FileToString(fileabove,true) will return: |
--------------------------------------------------------------------------
Hello, this is my neat little example#of using##a text file to a string!
--------------------------------------------------------------------------
Or |
--------------------------------------------------------------------------
Hello, this is my neat little exampleof usinga text file to a string!
--------------------------------------------------------------------------
*/[/codebox]

I know i'm beaten by him, but mine is a little more commented :D
Only a little though ;)

Edited by RocketLauncher3G, 17 January 2010 - 06:24 AM.

  • 0

#4 ramses12

ramses12

    6

  • GMC Member
  • 5769 posts
  • Version:GM8.1

Posted 17 January 2010 - 11:57 AM

You can do it without using 2 different temp variables for filename and file index.
var f,str;

f=argument0;

f=file_text_open_read(f);

str=file_text_read_string(f);

file_text_readln(f);

while(!file_text_eof(f)){

str+=chr(13)+chr(10)+file_text_read_string(f);

file_text_readln(f);

};

file_text_close(f);

return str;

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users