function write_file(filename) // // Create a file on disk, and then ask the user for data. // Copy everything the user types into the file. // Quit when the user types the Return key (an empty line), // signalling no more data. // // Arguments: // // filename (input) name of disk file to create // // first, we try to open the file for writing (which will // create it if it doesn't exist yet) fid = mopen(filename, 'w'); if (fid == -1) error('write_file: cannot open file for writing'); end done_yet = 0; while (done_yet == 0) // we use the optional second argument 's' to the INPUT function, // which means that everything the user types is kept together // as a string, instead of being broken up into an array of values. user_string = input('gimme stuff for the file (blank line to quit) ','s'); // if the user's string is equalt to a single space, // then the user typed nothing but a carriage return, so quit if (user_string == " ") then done_yet = 1; else mfprintf(fid, '%s\n', user_string); end end // remember to close the file before we return; // otherwise, some of the material might not be written onto disk mclose(fid); endfunction