使用C从文本文件中删除多个字符 [英] deleting multiple characters from text file using C

查看:77
本文介绍了使用C从文本文件中删除多个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文本文件中删除特定的字符串.我必须从文件[Ipsum,打印]中删除两个字符串.我尝试首先仅从文件中删除第一个字符串.但是字符串不能删除.我在犯错的地方无法更正我的代码.

#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;
  }

解决方案

首先,您的输入文件使用代表write的参数w打开,因此它将清除输入文件的内容.输入无用.

如果在行的末尾或读取的1000个字符的末尾之前\ 0,则代码也会生成符号(如果您未写整行或1000个字符,则它将读取其余内容作为符号). /p>

最终密码

#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  

I am trying to delete specific strings from the text file. I've to delete two strings from the file [Ipsum, printing]. I tried first deleting only first string from the file. But the string can not be deleted. I am unable to correct my code, where I am making mistake.

#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;
  }

解决方案

First of all, your input file is open with the argument w that stands for write,so it will clean the content of the input file making the input useless .

Also your code generates symbols if before the end of the line or before the end of 1000 chars readed are \0 (if you didnt write an entire line or 1000 chars it will read the rest of the content as symbols).

Final code

#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;
  }

Input:

THIS IS SPARTAAAAAAAAAAAAAA 

Output:

 IS SPARTAAAAAAAAAAAAAA  

这篇关于使用C从文本文件中删除多个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆