[MEL] Read/write file issue


#1

Hi,
In the script I want to write, my aim is to add some text in a file (.js, but could be .txt or anything else) at a specific line. So I wanted put each line in an string array, and when I reach the line I am looking for, add the new text, then re-write the file with the new line. Works fine so far, the file written by the script looks exactly like I wanted. Except that when Maya reads the new file with the fgetline command, i’ve got only the first line of this new file. So if I launch the script once again, the new file has only one line…
I’ve tried to substitute
with
, but it doesn’t change anything…
Please help !
Here’s my code (here, I just re-write without adding anything, and read the new file):

proc CheckFile (string $file)
{
	int $fileId = `fopen $file "r"`;
	string $nextLine = `fgetline $fileId`;
	string $all [] ;
	$all [0] = $nextLine;
	while (size ($nextLine)>0)
	{
		$nextLine = `fgetline $fileId`;
		$all [size ($all)] = $nextLine;
	}
	print $all;
	fclose $fileId;
}

global proc WriteFile ()
{
	string $myFile = "D:/Path/File.js";
	int $fileId = `fopen $myFile "r"`;
	string $nextLine = `fgetline $fileId`;
	string $all [] ;
	$all [0] = $nextLine;
	while (size ($nextLine)>0)
	{
		$nextLine = `fgetline $fileId`;
		$all [size ($all)] = $nextLine;
	}
	fclose $fileId;
		
	int $fileIdWrite = `fopen $myFile "w"`;
	for ($i = 0 ; $i < size ($all) ; $i++)
	{
		string $newLine = `substitute "
" $all [$i] "
"`;
		fwrite $fileIdWrite $newLine;
	}
	fclose $fileIdWrite;

	CheckFile ($myFile);
}