【代码】
#include <stdio.h>
//编写一个程序,打开文本文件,将其中所有的t字符删掉,然后输出该内容
void main()
{
FILE* fp;
fp = fopen("abc.txt", "r+");
if (fp == NULL)
{
perror("cannot open the file!\n");
exit(0);
}
char str[20];
fgets(str, 20, fp);
fclose(fp);
for (int i = 0; i < 20; i++)
{
if (str[i] == 't') {
str[i] = str[i + 1]; //删除该元素
for (int j = i; j < 19; j++)
{
str[j] = str[j + 1]; //这里相当于顺序表元素的删除,删除一个元素,后面所有的元素都要向前移动
}
i--; //将后面的元素移动一次后,就需要从这个位置重新判断
}
}
printf("%s", str);
}
txt文件中原始内容:
【运行结果】