为什么如下程序段在输入插入位置时输入0,插入的位置是第二个字符之后:// 插入函数 void insert_file(FILE *fp, char *fname) { char ch; long int pos; int len = 0; // 获取用户输入的文件名 printf("\t请输入插入的文件名:\n\t"); // 从标准输入中获取文件名 fgets(fname, MAX_LEN, stdin); // 查找'\n'字符并替换为'\0' char *pos1 = NULL; if ((pos1 = strchr(fname, '\n')) != NULL) *pos1 = '\0'; // 添加后缀名".txt",如果没有输入 if (!strstr(fname, ".txt")) strcat(fname, ".txt"); // 打开文件并判断是否打开成功 if ((fp = fopen(fname, "r+")) == NULL) { // 打开文件方式改为"r+",即可读可写 printf("\t不能打开文件!\n"); exit(0); } // 获取文件长度 fseek(fp, 0, SEEK_END); len = ftell(fp); // 显示文件已有内容 printf("\t文件已有内容:\n"); rewind(fp); while ((ch = fgetc(fp)) != EOF) { putchar(ch); } // 获取用户输入的插入位置 printf("\n\t请输入插入的位置(从0开始):\n\t"); scanf("%ld", &pos); getchar(); // 读取换行符 // 将文件指针移动到指定位置 fseek(fp, pos, SEEK_SET); // 依次将后面的字符往后移动1个字符 for (int i = len; i >= pos + 1; i--) { fseek(fp, i - 1, SEEK_SET); ch = fgetc(fp); fseek(fp, i, SEEK_SET); fputc(ch, fp); } // 获取用户输入的插入内容并写入到文件中 printf("\t请输入要插入的内容:\n\t"); ch = getchar(); while (ch != '#') { fputc(ch, fp); ch = getchar(); } // 关闭文件 fclose(fp); printf("\n\t插入成功!\n"); }
时间: 2024-03-30 11:34:45 浏览: 71
在该程序中,文件指针首先被设置为用户输入的插入位置,即 pos,然后在往后遍历时,从文件末尾开始往前遍历,最后将 pos 位置后面的字符全部往后移动一位,这样在 pos 位置处就留出了一个空位,可以插入新的字符。因此,如果输入插入位置为0时,实际上是将第一个字符往后移动一位,空出一个位置给插入的字符,所以插入的位置是第二个字符之后。
相关问题
修改此程序段,并给这段代码添加中文注释,要求插入位置的选择改为用鼠标光标选择:// 插入函数 void insert_file(FILE *fp, char *fname) { char ch; long int pos; // 获取用户输入的文件名 printf("\t请输入插入的文件名:\n\t"); // 从标准输入中获取文件名 fgets(fname, MAX_LEN, stdin); // 查找'\n'字符并替换为'\0' char *pos1 = NULL; if ((pos1 = strchr(fname, '\n')) != NULL) *pos1 = '\0'; // 添加后缀名".txt",如果没有输入 if (!strstr(fname, ".txt")) strcat(fname, ".txt"); // 打开文件并判断是否打开成功 if ((fp = fopen(fname, "a+")) == NULL) { printf("\t不能打开文件!\n"); exit(0); } // 获取用户输入的插入位置 printf("\t请输入要插入的位置:\n\t"); scanf("%ld", &pos); // 消耗掉输入缓冲区中的回车符 getchar(); // 将文件指针移动到指定位置 fseek(fp, pos, SEEK_SET); // 获取用户输入的插入内容并写入文件中 printf("\t请输入要插入的内容:\n\t"); ch = getchar(); while (ch != '#') { fwrite(&ch, sizeof(char), 1, fp); ch = getchar(); } printf("\n\n"); getchar(); fclose(fp); }
// 插入函数
void insert_file(FILE *fp, char *fname) {
char ch;
long int pos;
// 获取用户输入的文件名
printf("\t请输入插入的文件名:\n\t");
// 从标准输入中获取文件名
fgets(fname, MAX_LEN, stdin);
// 查找'\n'字符并替换为'\0'
char *pos1 = NULL;
if ((pos1 = strchr(fname, '\n')) != NULL)
*pos1 = '\0';
// 添加后缀名".txt",如果没有输入
if (!strstr(fname, ".txt"))
strcat(fname, ".txt");
// 打开文件并判断是否打开成功
if ((fp = fopen(fname, "a+")) == NULL) {
printf("\t不能打开文件!\n");
exit(0);
}
// 获取用户输入的插入位置
printf("\t请使用鼠标在文件中选择要插入的位置:\n\t");
// 将文件指针移动到指定位置
fseek(fp, pos, SEEK_SET);
// 获取用户输入的插入内容并写入文件中
printf("\t请输入要插入的内容:\n\t");
ch = getchar();
while (ch != '#') {
fwrite(&ch, sizeof(char), 1, fp);
ch = getchar();
}
printf("\n\n");
getchar();
fclose(fp);
}
修改此程序段,并给这段代码添加中文注释,要求显示文件已有内容并可用鼠标光标选择在其中的插入位置:// 插入函数 void insert_file(FILE *fp, char *fname) { char ch; long int pos; // 获取用户输入的文件名 printf("\t请输入插入的文件名:\n\t"); // 从标准输入中获取文件名 fgets(fname, MAX_LEN, stdin); // 查找'\n'字符并替换为'\0' char *pos1 = NULL; if ((pos1 = strchr(fname, '\n')) != NULL) *pos1 = '\0'; // 添加后缀名".txt",如果没有输入 if (!strstr(fname, ".txt")) strcat(fname, ".txt"); // 打开文件并判断是否打开成功 if ((fp = fopen(fname, "a+")) == NULL) { printf("\t不能打开文件!\n"); exit(0); } // 获取用户输入的插入位置 printf("\t请输入要插入的位置:\n\t"); scanf("%ld", &pos); // 消耗掉输入缓冲区中的回车符 getchar(); // 将文件指针移动到指定位置 fseek(fp, pos, SEEK_SET); // 获取用户输入的插入内容并写入文件中 printf("\t请输入要插入的内容:\n\t"); ch = getchar(); while (ch != '#') { fwrite(&ch, sizeof(char), 1, fp); ch = getchar(); } printf("\n\n"); getchar(); fclose(fp); }
// 插入函数
void insert_file(FILE *fp, char *fname) {
char ch;
long int pos;
// 获取用户输入的文件名
printf("\t请输入插入的文件名:\n\t");
// 从标准输入中获取文件名
fgets(fname, MAX_LEN, stdin);
// 查找'\n'字符并替换为'\0'
char *pos1 = NULL;
if ((pos1 = strchr(fname, '\n')) != NULL)
*pos1 = '\0';
// 添加后缀名".txt",如果没有输入
if (!strstr(fname, ".txt"))
strcat(fname, ".txt");
// 打开文件并判断是否打开成功
if ((fp = fopen(fname, "a+")) == NULL) {
printf("\t不能打开文件!\n");
exit(0);
}
// 显示文件已有内容
printf("\t文件已有内容:\n");
rewind(fp);
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
}
// 获取用户输入的插入位置
printf("\n\t请使用鼠标在文件中选择要插入的位置:\n\t");
// 将文件指针移动到指定位置
fseek(fp, pos, SEEK_SET);
// 获取用户输入的插入内容并写入文件中
printf("\t请输入要插入的内容:\n\t");
ch = getchar();
while (ch != '#') {
fwrite(&ch, sizeof(char), 1, fp);
ch = getchar();
}
printf("\n\n");
getchar();
fclose(fp);
}
阅读全文
相关推荐
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)