2017-04-16 9 views
-1

テキストファイルから特定の文字列を削除しようとしています。ファイル[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; 
    } 
+1

'はstrstr()'と 'MEMMOVE()'あなたの友人がいます。 (ただし、新しい文字列にコピーすることもできます) – wildplasser

+0

'' w "' - > '" w + "'。 'fflush'と' rewind'を実行します。 – BLUEPIXY

答えて

1

まず第一に、あなたの入力ファイルはwriteの略引数wとオープンしているので、入力が無用作り、入力ファイルの内容をきれいにします。

また、行末までまたは1000文字の最後が\ 0になる前にコードが生成されます(行全体または1000文字を記述しなかった場合、残りの内容はシンボルとして読み取られます)。

最終的なコード

#include <stdio.h> 
#include <stdlib.h> 
    int main() { 
    int j = 0, i; 
    FILE * fptr, * fp2; 
    char a[1024], temp[1024]; 

    char *key = "THIS", *textDelete_2 = "IS"; 

    fptr = fopen("test.txt", "r"); 
    if (fptr == NULL) { 
     printf("File can not be opened. \n"); 
     exit(0); 
    } 

    fp2 = fopen("temp.txt", "w"); 
    if (fp2 == NULL) { 
     printf("File fp2 can not be opened. \n"); 
     exit(0); 
    } 
    printf("\n processing ... \n"); 

    while (fgets(a, sizeof(a), fptr)) { 
     for (i = 0; a[i] != '\0'; ++i) { 
      if (a[i] == 0)break; 
     if (a[i] == ' ') { 
      temp[j] = 0; 
      if (strcmp(temp, key) != 0) { 
      fputs(temp, fp2); 
      } 
      j = 0; 

      fputs(" ", fp2); 
     } else { 
      temp[j++] = a[i]; 
     } 
     } 

     for (i = 0; i < strlen(temp); i++){ 

      if (!isalpha(temp[i]))temp[i] = ' '; 
     } 
     if (strcmp(temp, key) != 0) { 
     fputs(temp, fp2); 
     } 
     fputs("\n", fp2); 
     a[0] = 0; 
    } 

    fclose(fptr); 
    fclose(fp2); 
    printf("\n processing completed"); 
    getchar(); 
    return 0; 
    } 

入力:

THIS IS SPARTAAAAAAAAAAAAAA 

出力:

IS SPARTAAAAAAAAAAAAAA 
+0

これは1つの文字列に対して機能しましたが、複数の文字列をどのようにして削除できますか?私はこれとISを削除しなければならないように。あなたのコードを修正しようとしましたが、正しい結果が得られません。助けてください!!! – nischalinn

+0

@nischalinn引数 "a"を要求して2回呼び出す関数にwhileループを入れます( "a"は削除される文字列です) –

関連する問題