Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: on Sat 23/02/2008 18:39:08

Title: Modify file
Post by: on Sat 23/02/2008 18:39:08
I wish to modify a single character in a text file

I tried:

  File *input = File.Open(filetoload, eFileRead);
  File *output = File.Open(filetoload, eFileWrite);


"copying" the file on itself (apart from the values I have to modify)

unfortunately this does not work (the file is filled with lines of a strange character).

Where is the mistake?
Title: Re: Modify file
Post by: Dualnames on Sat 23/02/2008 21:31:05
I recall that you need an extra thing like File.WriteInt() or whatever or single char or something.
Title: Re: Modify file
Post by: on Sat 23/02/2008 22:17:02
Quote from: Dualnames on Sat 23/02/2008 21:31:05
I recall that you need an extra thing like File.WriteInt() or whatever or single char or something.

-.-" yes yes, I've done that (used raw writechar). I don't see in the manual a function to just modify a file but I hope there's a way get past around that.
Title: Re: Modify file
Post by: Radiant on Sat 23/02/2008 22:50:59
It's probably not wise, and certainly not necessary here, to open the same file twice simultaneously.
Title: Re: Modify file
Post by: on Sat 23/02/2008 23:12:12
Quote from: Radiant on Sat 23/02/2008 22:50:59
It's probably not wise, and certainly not necessary here, to open the same file twice simultaneously.

Not wise, not necessary, duly noted. But then again, what's the smooth solution? Is there an eSomething I've missed?
Title: Re: Modify file
Post by: Radiant on Sat 23/02/2008 23:52:31
Yes, you're missing the code that reads the file, kind of like


  while (not EOF file 1) {  write (file2, read (file1)); }

Title: Re: Modify file
Post by: on Sun 24/02/2008 03:20:36
Quote from: bicilotti link=topic=33825.msg439427#msg439427
-.-" yes yes, I've done that (used raw writechar). I don't see in the manual a function to just modify a file but I hope there's a way get past around that.

I've posted just the first two lines of the code (the other ones I don't post because they're a simple while loop I've used with plain eRead/eWrite files and they give no prob). For simplicity's sake we can use:


  char c = input.ReadRawChar();
  output.WriteRawChar(c);
  //copy the first char

     c = input.ReadRawChar();
  output.WriteRawChar('a');
  //place an 'a' in second place


I'll try to be crystal clear: I need to modify a single character in a file. A banal solution is:

-open tomodify in eRead
-open dummy in eWrite
-copy the content of tomodify in dummy whith a simple while loop modifying the necessary characters
-copy the content of dummy to tomodify.

That I don't like particularly, because it need a second file to be created (second file I cannot dispose of).

question: is there another path to accomplish that task?
Title: Re: Modify file
Post by: Khris on Sun 24/02/2008 03:37:41
No.

Download the latest beta, if features a File.Delete command.
Title: Re: Modify file
Post by: on Sun 24/02/2008 04:04:34
Quote from: KhrisMUC on Sun 24/02/2008 03:37:41
Download the latest beta, if features a File.Delete command.

Exactly what I need.
Title: Re: Modify file
Post by: Radiant on Sun 24/02/2008 11:14:36
Quote from: bicilotti on Sun 24/02/2008 03:20:36
-open tomodify in eRead
-open dummy in eWrite
-copy the content of tomodify in dummy whith a simple while loop modifying the necessary characters
-copy the content of dummy to tomodify.

question: is there another path to accomplish that task?

Yes.

-open tomodify in eRead
-copy content to an array in memory
-modify array in memory as needed
-close tomodify, then open it again in eWrite
-write array back to tomodify
Title: Re: Modify file
Post by: Pumaman on Sun 24/02/2008 13:53:02
You can't write to a file and read it at the same time, or it can cause unpredictable results (as you've noticed!)
Title: Re: Modify file
Post by: monkey0506 on Sun 24/02/2008 22:29:46
Quote from: Radiant on Sun 24/02/2008 11:14:36-copy content to an array in memory

On this note, would it be possible to implement a way to retrieve the length (in bytes) of a file? Something like File.Length or File.Size would be useful.
Title: Re: Modify file
Post by: Radiant on Mon 25/02/2008 01:21:54
Quote from: monkey_05_06 on Sun 24/02/2008 22:29:46
On this note, would it be possible to implement a way to retrieve the length (in bytes) of a file? Something like File.Length or File.Size would be useful.

Technically, you can fake that by opening it in raw (binary) mode, reading raw characters until you reach the end, and counting them.

File.Size would still be nice, as would File.Seek, though.
Title: Re: Modify file
Post by: monkey0506 on Mon 25/02/2008 01:46:26
I knew about this method of getting the file size, but I figured it might be possible for AGS to grab this information directly making it a bit faster than having to manually count all the bytes.

File.Seek would make it possible to "prepend" to files which would be nice.