优化以下代码:void saveFile() { int n; FILE *fp; enterFileNmae(); //input the file name; fp=fopen(fname,"w") ; //writing if(fp==NULL) return ; for(n=0;n<lines;n++) fprintf(fp,"%s\n",buf[n]); //output on the file fclose(fp); //close the file; printf("已保存。\n"); }
时间: 2024-03-10 15:46:27 浏览: 59
可以对该函数进行以下优化:
1. 将函数中的一些变量声明放到函数顶部,以提高代码可读性。
2. 检查文件打开是否成功,如果不成功,应该输出错误信息。
3. 在写入文件时,应该使用fwrite()函数代替fprintf()函数,以提高效率。
4. 在文件保存完成后,应该输出保存的文件名,以提高用户体验。
下面是优化后的代码:
```
void saveFile() {
int n;
FILE *fp;
char filename[100];
enterFileName(filename); //input the file name;
fp = fopen(filename, "w");
if(fp == NULL) {
printf("文件打开失败!\n");
return;
}
for(n = 0; n < lines; n++) {
fwrite(buf[n], sizeof(char), strlen(buf[n]), fp); //output on the file
fwrite("\n", sizeof(char), 1, fp);
}
fclose(fp); //close the file;
printf("文件 %s 已保存。\n", filename);
}
```
相关问题
生成以下代码的流程图void PHUM(char *file,char *s) FILE *fp; int i=0; printf ("save your Huffmancode to the file:"); scanf("%s",file); if((fp=fopen(file,"w"))==NULL) printf("faild\n"); while(s[i]!='\0') { // fwrite(s,1,strlen(s),fp); // fprintf(fp,'%c',s[i]); fprintf(fp,"%c",s[i]); i++; } fclose(fp); printf("write over\n"); }
```
+------------------+
| Start Program |
+------------------+
|
V
+-----------+
| Read Input|
+-----------+
|
V
+-----------------------+
| Prompt for File Name |
+-----------------------+
|
V
+-----------------------+
| Prompt for String |
+-----------------------+
|
V
+-----------------------+
| Open File for Writing |
+-----------------------+
|
V
+-----------------------+
| Loop through String |
+-----------------------+
|
V
+-----------------------+
| Write Character to File|
+-----------------------+
|
V
+-----------------------+
| Close File |
+-----------------------+
|
V
+-----------------------+
| Print "Write Over" |
+-----------------------+
|
V
+------------------+
| End Program |
+------------------+
```
有五个学生,每个学生有三门课的成绩,从键盘上输入学生的学号,姓名,三门课成绩,把它们存放到磁盘文件中,并从该文件中能读出显示。 #include <stdio.h> #include <stdlib.h> struct student { int number; char name[20]; int score[3]; } stud[5]; void save() { FILE *fp; int i; if (( fp = fopen( "student.dat", "wb" )) == NULL ) { printf( "can not open file\n" ); exit( 0 ); } for ( i = 0; i < 5; i++ ) { fwrite(&stud[i],sizeof(struct student),1,fp); } fclose( fp ); } void load() { } int main() { int i, j; for ( i = 0; i < 5; i++ ) { printf( "\ninput %d student's number: ", i + 1 ); scanf( "%d", &stud[i].number ); printf( "\ninput %d student's name: ", i + 1 ); scanf( "%s", stud[i].name ); printf( "\ninput %d student's score: ", i + 1 ); for ( j = 0; j < 3; j++ ) { scanf( "%d", &stud[i].score[j] ); } } save(); load(); }
好的,这是一个C语言程序,用于存储和读取五个学生的学号、姓名和三门课成绩。程序中包含了两个函数:save和load。save函数用于将学生信息保存到磁盘文件中,load函数用于从磁盘文件中读取学生信息并显示出来。目前load函数还没有实现,需要你来完成它。具体的程序实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
struct student
{
int number;
char name[20];
int score[3];
} stud[5];
void save()
{
FILE *fp;
int i;
if (( fp = fopen( "student.dat", "wb" )) == NULL )
{
printf( "can not open file\n" );
exit( 0 );
}
for ( i = 0; i < 5; i++ )
{
fwrite(&stud[i],sizeof(struct student),1,fp);
}
fclose( fp );
}
void load()
{
FILE *fp;
int i;
if (( fp = fopen("student.dat", "rb")) == NULL)
{
printf("can not open file\n");
exit(0);
}
for (i = 0; i < 5; i++)
{
fread(&stud[i], sizeof(struct student), 1, fp); // 读取文件中的学生信息
printf("number: %d\nname: %s\nscore: %d %d %d\n", stud[i].number, stud[i].name, stud[i].score[0], stud[i].score[1], stud[i].score[2]); // 显示读取的学生信息
}
fclose(fp);
}
int main()
{
int i, j;
for ( i = 0; i < 5; i++ )
{
printf( "\ninput %d student's number: ", i + 1 );
scanf( "%d", &stud[i].number );
printf( "\ninput %d student's name: ", i + 1 );
scanf( "%s", stud[i].name );
printf( "\ninput %d student's score: ", i + 1 );
for ( j = 0; j < 3; j++ )
{
scanf( "%d", &stud[i].score[j] );
}
}
save();
load();
return 0;
}
```
在这个程序中,load函数的实现比较简单,它只需要打开文件,循环读取文件中的学生信息,同时显示出来即可。需要注意的是,读取文件时使用的是fread函数,参数与保存文件时使用的fwrite函数相同,分别为:要读取的数据地址、每个数据项的大小、数据项个数和文件指针。
阅读全文