テキストファイルから特定の文字列を削除しようとしています。ファイル[Ipsum、printing]から2つの文字列を削除しました。私は最初にファイルから最初の文字列を削除しようとしました。しかし、文字列は削除できません。私は間違いを犯しているコードを修正することができません。Cを使用してテキストファイルから複数の文字を削除する
#include <stdio.h>
#include <stdlib.h>
int main() {
int j = 0, i;
char getText[1000] = "Lorem Ipsum is simply dummy text of the printing and typesetting industry";
FILE * fptr, * fp2;
char a[1000], temp[1000];
char key[50] = "Ipsum", textDelete_2[50] = "printing";
fptr = fopen("D:\\test.txt", "w");
if (fptr == NULL) {
printf("File can not be opened. \n");
exit(0);
}
fputs(getText, fptr);
fp2 = fopen("D:\\temp.txt", "w");
if (fp2 == NULL) {
printf("File fp2 can not be opened. \n");
exit(0);
}
printf("\n processing ... \n");
while (fgets(a,1000,fptr)) {
for (i = 0; a[i] != '\0'; ++i) {
if (a[i] == ' ') {
temp[j] = 0;
if (strcmp(temp, key) != 0) {
fputs(temp, fp2);
}
j = 0;
fputs(" ", fp2);
} else {
temp[j++] = a[i];
}
}
if (strcmp(temp, key) != 0) {
fputs(temp, fp2);
}
fputs("\n", fp2);
a[0] = 0;
}
fclose(fptr);
fclose(fp2);
printf("\n processing completed");
return 0;
}
'はstrstr()'と 'MEMMOVE()'あなたの友人がいます。 (ただし、新しい文字列にコピーすることもできます) – wildplasser
'' w "' - > '" w + "'。 'fflush'と' rewind'を実行します。 – BLUEPIXY