打开一个输出文件-大学物理知识点总结
第八章输入/输出函数8.8文件位置和状态函数第240页函数exist的第二种模式搜索由参数kind指定的条目。它的合法类型为“var”,“file”,“builtin”和“dir”。函数exist是非常重要的,因为我们可以利用它判断一个文件是否存在。当文件被打开时,fopen函数中权限字符串“w”和“w+”会删除文件已有的一个文件。在程序员允许fopen函数删除一个文件时,它必须征得用户的同意。表8.12由函数exist的返回值值意义0没有发现条目1条目为当前工作区的一个变量2条目为m文件或未知类型的文件3条目是一个MEX文件4条目是一个MDL文件5条目是一个内建函数6条目是一个p代码文件7条目是一个目录例8.4打开一个输出文件这个程序从用户那里得到输出文件名,并检查它是否存在。如果存在,就询问用户是要把用新数据覆盖这个文件,还是要把新的数据添加到这个文件中。如果这个文件不存在,那么这个程序就会很容易地打开输出文件。
想要了解更多关于文件操作的实例和技巧吗?可以参考一些相关资源。您可以查看《Java学习笔记基本输入输出数据操作实例分析》,其中详细介绍了Java中的输入输出操作。对于Perl语言爱好者,《Perl学习笔记之文件操作》也提供了深入的讲解。如果您更偏向于前端开发,可以阅读《JavaScript学习笔记之数组基本操作示例》,了解更多关于JavaScript中的数组操作。同样,PHP开发者也不容错过《PHP输入输出流学习笔记》。
% Script file: output.m
%
% Purpose:
% To demonstrate opening an output file properly.
% This program checks for the existence of an output
% file. If it exists, the program checks to see if
% the old file should be deleted, or if the new data
% should be appended to the old file.
%
% Record of revisions:
% Date Programmer Description of change
% ======= ===
% 11/29/98 S. J. Chapman Original code
%
% Define variables:
% fid -- File id
% out_filename -- Output file name
% yn -- Yes/No response
% Get the output file name.
out_filename = input('Enter output filename: ','s');
% Check to see if the file exists.
if exist(out_filename,'file')
% The file exists
disp('Output file already exists.');
yn = input('Keep existing file? (y/n) ','s');
if yn == 'n'
fid = fopen(out_filename,'wt');
else
fid = fopen(out_filename,'at');
end
else
是否发现新知识让人心潮澎湃呢?希望这些资源能帮助您更好地理解和应用文件操作技巧,让您的编程之路更加顺畅!