Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中的文件处理-从文本文件列表中删除特定单词_C_File Handling - Fatal编程技术网

C语言中的文件处理-从文本文件列表中删除特定单词

C语言中的文件处理-从文本文件列表中删除特定单词,c,file-handling,C,File Handling,我正在使用以下代码从我的基本C程序填充一个简短的字典: void main () { FILE *fp; fp = fopen("c:\\CTEMP\\Dictionary2.txt", "w+"); fprintf(fp, Word to Dictionary"); printf("Enter file name: "); scanf("%s", filename); //open file in read mode fileptr1

我正在使用以下代码从我的基本C程序填充一个简短的字典:

void main () {

FILE *fp;

fp = fopen("c:\\CTEMP\\Dictionary2.txt", "w+"); 

fprintf(fp, Word to Dictionary");
printf("Enter file name: ");
        scanf("%s", filename);
        //open file in read mode
        fileptr1 = fopen("c:\\CTEMP\\Dictionary.txt", "r");
        ch = getc(fileptr1);
        while (ch != EOF)
        {
            printf("%c", ch);
            ch = getc(fileptr1);
        }
        //rewind
        rewind(fileptr1);
        printf(" \n Enter line number of the line to be deleted:");
        scanf("%d", &delete_line);
        //open new file in write mode
        fileptr2 = fopen("replica.c", "w");
        ch = getc(fileptr1);
        while (ch != EOF)
        {
            ch = getc(fileptr1);
            if (ch == '\n')
            {
                temp++;
            }
            //except the line to be deleted
            if (temp != delete_line)
            {
                //copy all lines in file replica.c
                putc(ch, fileptr2);
            }
        }
        fclose(fileptr1);
        fclose(fileptr2);
        remove("c:\\CTEMP\\Dictionary.txt");
        //rename the file replica.c to original name
        rename("replica.c", "c:\\CTEMP\\Dictionary.txt");
        printf("\n The contents of file after being modified are as follows:\n");
        fileptr1 = fopen("c:\\CTEMP\\Dictionary.txt", "r");
        ch = getc(fileptr1);
        while (ch != EOF)
        {
            printf("%c", ch);
            ch = getc(fileptr1);
        }
        fclose(fileptr1);
        scanf_s("%d");
        return 0;

    }
然而,我也希望删除某些我不希望再出现在字典中的单词。我做了一些研究,我知道这一点

您不能从文件中删除内容并将剩余内容下移。您只能追加、截断或覆盖


最好的选择是将文件读入内存,在内存中进行处理,然后将其写回磁盘“

如果没有要删除的单词,如何创建新文件

谢谢

  • 你打开两个文件:一个是你拥有的(用于阅读)文件,另一个是新的(用于阅读) 写作)
  • 依次读取第一个文件中的每一行
  • 你将每行的内容与你需要的单词进行比较 删除
  • 如果行与任何删除词不匹配,则 您将其写入新文件

如果您需要执行的操作要复杂得多,那么您可以使用mmap()将其直接“读入内存”,但这是一种更高级的技术;您需要将文件视为一个没有零终止符的字节数组,有很多方法可以将其搞乱。

我使用了以下代码:

void main () {

FILE *fp;

fp = fopen("c:\\CTEMP\\Dictionary2.txt", "w+"); 

fprintf(fp, Word to Dictionary");
printf("Enter file name: ");
        scanf("%s", filename);
        //open file in read mode
        fileptr1 = fopen("c:\\CTEMP\\Dictionary.txt", "r");
        ch = getc(fileptr1);
        while (ch != EOF)
        {
            printf("%c", ch);
            ch = getc(fileptr1);
        }
        //rewind
        rewind(fileptr1);
        printf(" \n Enter line number of the line to be deleted:");
        scanf("%d", &delete_line);
        //open new file in write mode
        fileptr2 = fopen("replica.c", "w");
        ch = getc(fileptr1);
        while (ch != EOF)
        {
            ch = getc(fileptr1);
            if (ch == '\n')
            {
                temp++;
            }
            //except the line to be deleted
            if (temp != delete_line)
            {
                //copy all lines in file replica.c
                putc(ch, fileptr2);
            }
        }
        fclose(fileptr1);
        fclose(fileptr2);
        remove("c:\\CTEMP\\Dictionary.txt");
        //rename the file replica.c to original name
        rename("replica.c", "c:\\CTEMP\\Dictionary.txt");
        printf("\n The contents of file after being modified are as follows:\n");
        fileptr1 = fopen("c:\\CTEMP\\Dictionary.txt", "r");
        ch = getc(fileptr1);
        while (ch != EOF)
        {
            printf("%c", ch);
            ch = getc(fileptr1);
        }
        fclose(fileptr1);
        scanf_s("%d");
        return 0;

    }

逐字读取源文件,对于所需的每个字,将其写入输出。“最好的选择是将文件读入内存,在内存中处理,然后将其写回磁盘。”部分原因是没有将您不需要的数据写入文件。@Jean BaptisteYunès和@Some programmer dude感谢您的贡献。感谢@Richard Urwin的输入。我的操作并没有那么复杂,所以我将使用你的第一个方法。当你完成后,你将得到两个文件:旧文件和新文件。您可能希望用新文件替换旧文件,但有一种模式并不明显:关闭两个文件后,首先重命名旧文件。然后将新文件重命名为原始名称,然后删除旧文件。如果你不这样做,并且出现问题,你可能会丢失所有的数据。嗨,Richard,你介意我问你几个关于你刚才在聊天中所说的问题吗?