c语言 save函数

void save()
{
FILE *fp;
int i;
if((fp=fopen("user.txt","wb"))==NULL)
{printf("cannot open file\n");
return;}
for(i=0;i<MAX;i++)
if(fwrite(&user[i],sizeof(struct diary_type),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}

这是我写的save函数,作用是将stu的几个输入的数据保存在 stu_list的文件上.
我想问下,怎么修改才能达到这样的效果: 保存的那个文件的文件名可以自己定义,比如我可以输入一些数字,保存在叫mark的文件里, 输入另外的则保存在其他文件里,文件名字可以自己定义,可以有形参!
谢谢了
错了,是保存在user.txt的文件上

  可换成
void save()
{
char name[10];
FILE *fp;
int i;
scanf("%s",name)
if((fp=fopen(name,"wb"))==NULL)
{printf("cannot open file\n");
return;}
for(i=0;i<MAX;i++)
if(fwrite(&user[i],sizeof(struct diary_type),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
  相关概念:
  文件: 按一定规则存储在磁盘上的数据集合。
  文件名: 能唯一标识某个磁盘文件的字符串。形式: 盘符:/ 路径 / 文件名.扩展名
  文本文件:: 数据以其数字字符的ASCII码形式、一个字节一个字节地存储在磁盘上。
  二进制文件:数据以二进制形式在存储在磁盘上。
  设备文件:输入/输出设备
  标准输入文件:键盘
  标准输出文件/标准错误输出文件:显示器
  文件型指针:C语言是通过名为FILE的结构型指针来管理文件读写的。FILE *<变量名>
  文件的打开和关闭 :文件操作先建立文件与文件指针之间的关系,接着进行文件的读与写。建立文件与文件指针之间的联系的过程是文件的打开。终止这种联系就是文件的关闭
  char ch;
  ch = getchar () 功能等价于 ch = fgetc(stdin) 功能等价于 scanf("%c",ch) 功能等价于 fscanf(stdin, "%c",ch)
  putchar(ch) 功能等价于 printf("%c",ch) 功能等价于 fputc(stdout) 功能等价于 fprintf(stdout, "%c", ch)
  
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-05-06
可以撒,你只要定义个数组就可以了,如:
void save()
{
char name[10];
FILE *fp;
int i;
scanf("%s",name)
if((fp=fopen(name,"wb"))==NULL)
{printf("cannot open file\n");
return;}
for(i=0;i<MAX;i++)
if(fwrite(&user[i],sizeof(struct diary_type),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}本回答被提问者和网友采纳
第2个回答  2009-05-06
你就在user.txt那做手脚就行
你可以这样char a[10];
scanf(“%s“,a);if((fp=fopen(a,‘wb"))==NULL)
你想达到什么效果
我认为你说的就是 随意可存在自己输入的文件名下
第3个回答  2015-06-01
  save函数:
  保存当前工作空间的所有变量到文件名制定的文件中,此文件后缀名通常为mat。如果不指定文件名变量,则会默认保存到matlab.mat这个文件中的一种运算符法。
  函数编程:
  SAVE Save workspace variables to disk.
  SAVE FILENAME saves all workspace variables to the binary "MAT-file"
  named FILENAME.mat. The data may be retrieved with LOAD. If FILENAME
  has no extension, .mat is assumed.
  SAVE, by itself, creates the binary "MAT-file" named 'matlab.mat'.
  SAVE FILENAME X saves only X.
  SAVE FILENAME X Y Z saves X, Y, and Z. The wildcard '*' can be used to
  save only those variables that match a pattern.
  SAVE FILENAME -REGEXP PAT1 PAT2 can be used to save all variables
  matching the specified patterns using regular expressions. For more
  information on using regular expressions, type "doc regexp" at the
  command prompt.
  SAVE FILENAME -STRUCT S saves the fields of the scalar structure S as
  individual variables within the file FILENAME.
  SAVE FILENAME -STRUCT S X Y Z saves the fields S.X, S.Y and S.Z to
  FILENAME as individual variables X, Y and Z.
  SAVE FILENAME ... -APPEND adds the variables to an existing file
  (MAT-file only).
  Format Options:
  SAVE ... -ASCII uses 8-digit ASCII form instead of binary regardless
  of file extension.
  SAVE ... -ASCII -DOUBLE uses 16-digit ASCII form.
  SAVE ... -ASCII -TABS delimits with tabs.
  SAVE ... -ASCII -DOUBLE -TABS 16-digit, tab delimited.
  SAVE ... -MAT saves in MAT format regardless of extension.
  Version Compatibility Options:
  The following options enable you to save your workspace data to a
  MAT-file that can then be loaded into an earlier version of MATLAB.
  The resulting MAT-file supports only those data items and features
  that were available in this earlier version of MATLAB. (See the
  second table below for what is supported in each version.)
  Command Option | Saves a MAT-File That You Can Load In