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 = fopen(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 ','s'); % if the size of the user's string is zero, % then the user didn't type anything, and we should quit input_size = size(user_string); if (input_size(1) == 0) done_yet = 1; else fprintf(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 fclose(fid);