Mel Script - reading ascii files

 

 

 

All file IO in mel requires the use of fopen and fclose to open and close files on the hard drive. When reading the actual data, we can make use of either fgetword or fgetline to read words or lines at a time.

 

 

 

 

Example 1 : using fgetword

fgetword simply reads the next word in the file and returns it as a text string.

 

 

			
	{
		// construct the full path to a file
		$filename = (`internalVar -userPrefDir` + "windowPrefs.mel");
		
		// open the file for reading
		$file = `fopen $filename "r"`;

		// loop UNTIL we reach the end of the file
		while( !feof($file) )
		{
			// read a word from the file and print
			print( `fgetword $file` + "\n" );
		}
		
		// close the file
		fclose $file;
	}
		

 

 

Example 2 : using fgetline

fgetline simply reads a line of text from the file and returns it as a string.

 

 

			
	{
		// construct the full path to a file
		$filename = (`internalVar -userPrefDir` + "windowPrefs.mel");
		
		// open the file for reading
		$file = `fopen $filename "r"`;

		// loop UNTIL we reach the end of the file
		while( !feof($file) )
		{
			// read a word from the file and print
			print( `fgetline $file` + "\n" );
		}
		
		// close the file
		fclose $file;	
	}