Modify file

Started by , Sat 23/02/2008 18:39:08

Previous topic - Next topic

bicilotti

I wish to modify a single character in a text file

I tried:
Code: ags

  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?

Dualnames

I recall that you need an extra thing like File.WriteInt() or whatever or single char or something.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

bicilotti

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.

Radiant

It's probably not wise, and certainly not necessary here, to open the same file twice simultaneously.

bicilotti

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?

Radiant

Yes, you're missing the code that reads the file, kind of like

Code: ags

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


bicilotti

#6
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:

Code: ags

  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?

Khris

No.

Download the latest beta, if features a File.Delete command.

bicilotti

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.

Radiant

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

Pumaman

You can't write to a file and read it at the same time, or it can cause unpredictable results (as you've noticed!)

monkey0506

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.

Radiant

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.

monkey0506

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.

SMF spam blocked by CleanTalk