【发布时间】:2014-11-16 01:51:22
【问题描述】:
我创建的代码应该能够复制用户建议的文件。我想知道的是:如何设置输出文件模式以及如何确定此代码中的输出文件模式权限?
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
char source_file, target_file;
FILE *in, *out;
printf("Enter name of file to copy\n");
gets(source_file);
printf("Enter name of file to copy to\n");
gets(target_file);
in = (source, O_RDONLY);
out = (target_file, O_CREAT|WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
/* error handing */
if( in == NULL )
{
printf("Error. \n");
exit(0);
}
printf("Enter the copied file name \n");
gets(target_file);
out = fopen(target_file, "w");
/*error handing*/
if( out == NULL )
{
fclose(in);
printf("File opening error.\n");
exit(0);
}
while(( c = fgetc(in) ) != EOF )
fputc(c,out);
fclose(in);
fclose(out);
return 0;
}
【问题讨论】:
-
提示:您已经知道如何更改模式,您将其作为此问题的标签之一。另一方面,要获得文件权限,请使用
stat。 -
这会不会像在我关闭文件之前在某处添加 chmod(out, 666) 一样简单?
-
请不要使用
gets(),即使是在玩具程序中。它是致命的;这是一个非常不好的习惯。请改用fgets()。