求大神看看,为什么我这个程序每个小的子程序最后的结果不能写进文件?
#include<stdio.h>#include<string.h>
#include<stdlib.h>
char test[1000];
FILE *fp;
char address[100];//文件地址
void insert();//插入
void delete();//删除
void change();//替换
void find();//查找
int open();//打开文件
void display();//二级显示
int main()
{
int flag, flag1, len;
while (1)
{
printf("\t---------------------欢迎进入文本编辑器--------------------\n");
printf("\t------------------------1:打开文件-------------------------\n");
printf("\t------------------------2:编辑文件-------------------------\n");
printf("\t------------------------3:退出-----------------------------\n");
printf("\t-----------------------------------------------------------\n");
printf("\t请选择服务:\n\t");
scanf("%d", &flag);
switch (flag)
{
case 1:len = open();
printf("\t文件的内容为:\n\t");
for (int i = 0; i < len; i++)
{
printf("%c", test[i]);
}
printf("\n\n"); break;
case 2:display();
printf("\t请选择服务:\n\t");
scanf("%d", &flag1);
switch (flag1)
{
case 1:insert(); break;
case 2:delete(); break;
case 3:change(); break;
case 4:find(); break;
}
break;
case 3:return;
default:printf("\t输入错误!\n");
}
}
//fclose(fp);
return 0;
}
int open()
{
char /*address[100], */ch, input, num = 0;
int len = 0;
printf("\t请输入文件地址:\n\t");
scanf("%s", &address);
getchar();
fp = fopen(address, "r+");
if (!fp)
{
printf("\t该文件不存在!");
exit(1);
}
while ((ch = fgetc(fp)) != EOF)
{
test[len] = ch;
//putchar(ch);
len++;
}
//rewind(fp);
return len;
}
void insert()
{
int len = open();
char inputin;
FILE *fp;
int placein;//插入
//fp = fopen(address, "r+");
fp = fopen("F:\\A1.txt", "r+");
printf("\t请输入您要插入的字符:\n\t");
scanf("%c", &inputin);
printf("\t请输入您要插入的位置:\n\t");
scanf("%d", &placein);
if (placein > len)
printf("\t该位置不能插入!");
for (int i = len - 1; i >=placein-1; i--)
test[i+1] = test[i];
test[placein -1] = inputin;
len = len + 1;
printf("\t插入后文件的内容为:\n\t");
for (int i = 0; i < len; i++)
printf("%c", test[i]);
//rewind(fp);
for (int i = 0; i < len; i++)
fputc(test[i], fp);
printf("\n\n");
//fclose(fp);
}
void delete()
{
int len = open();
char inputde;
FILE *fp;
fp = fopen(address, "r+");
printf("\t请输入您要删除的字符:\n\t");
scanf("%c", &inputde);
for (int i = 0; i < len; i++)
{
if (test[i] == inputde)
{
for (int j = i; j < len; j++)
test[j] = test[j + 1];
len--;
}
}
printf("\t删除后文件的内容为:\n\t");
for (int i = 0; i < len; i++)
{
printf("%c", test[i]);
}
for (int i = 0; i < len; i++)
fputc(test[i], fp);
printf("\n\n");
//fclose(fp);
}
void change()
{
int len = open();
char inputchf, inputchb;
//FILE *fp1;
//fp1 = fp;
FILE *fp;
fp = fopen(address, "r+");
printf("\t请输入替换前字符:\n\t");
scanf("%c", &inputchf);
getchar();
printf("\t请输入替换后的字符:\n\t");
scanf("%c", &inputchb);
for (int i = 0; i < len; i++)
{
if (test[i] == inputchf)
test[i] = inputchb;
}
printf("\t替换后文件的内容为:\n\t");
for (int i = 0; i < len; i++)
{
printf("%c", test[i]);
}
for (int i = 0; i < len; i++)
fputc(test[i], fp);
printf("\n\n");
//fclose(fp);
}
void find()
{
char input;
int num = 0;
int len = open();
FILE *fp;
fp = fopen(address, "r+");
//printf("\t结果:\n");
printf("\t请输入需要查找的字符:\n\t");//查找
scanf("%c", &input);
getchar();
printf("\t结果:\n");
for (int i = 0; i < len; i++)
{
if (test[i] == input)
{
printf("\t你输入的字符在第%d位\n", i+1);
num++;
}
}
if (num == 0)
printf("\t您要查找的字符在该文件中不存在!");
printf("\n\n");
//fclose(fp);
}
void display()
{
printf("\t------------------------1:插入-----------------------------\n");
printf("\t------------------------2:删除-----------------------------\n");
printf("\t------------------------3:替换-----------------------------\n");
printf("\t------------------------4:查找-----------------------------\n");
}
//F:\\A1.txt