Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.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,大家好,我有一个文本文件,名为dictionary.txt。基本上我做的是两种选择的菜单,1。在字典和字典中添加生词。从字典中删除单词。现在我设法做了菜单并添加了新单词。然而,我一直坚持从文件中删除。我希望当用户输入例如“runners”时,在dictionary.txt中搜索单词并将其删除。告诉你我在学校还没有涉及到的一切,但我在这里寻找一些想法,以便我可以继续完成任务。我试过一些东西,但正如我已经告诉你的,我还没有涵盖它,所以我不知道如何真正做到这一点。我感谢所有的帮助。下面是我的程序 您可

大家好,我有一个文本文件,名为dictionary.txt。基本上我做的是两种选择的菜单,1。在字典和字典中添加生词。从字典中删除单词。现在我设法做了菜单并添加了新单词。然而,我一直坚持从文件中删除。我希望当用户输入例如“runners”时,在dictionary.txt中搜索单词并将其删除。告诉你我在学校还没有涉及到的一切,但我在这里寻找一些想法,以便我可以继续完成任务。我试过一些东西,但正如我已经告诉你的,我还没有涵盖它,所以我不知道如何真正做到这一点。我感谢所有的帮助。下面是我的程序



您可以以二进制模式打开文件,然后将内容加载到字符串或字符串数组中,然后对字符串进行搜索/删除/编辑,然后清除文件内容,最后将新内容写入文件。

  • 你打开两个文件:一个是你的(用于阅读)文件,另一个是新的(用于写作)
  • 依次读取第一个文件中的每一行
  • 将每行内容与需要删除的单词进行比较
  • 如果该行与任何删除字都不匹配,则将其写入新文件
Josuel,摘自Richard Urwin之前回答的问题

您可以使用以下代码:

#include <stdio.h>

    int main()
    {
        FILE *fileptr1, *fileptr2;
        char filename[40];
        char ch;
        int delete_line, temp = 1;

        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;

    }
#包括
int main()
{
文件*fileptr1,*fileptr2;
字符文件名[40];
char ch;
int delete_行,temp=1;
printf(“输入文件名:”);
scanf(“%s”,文件名);
//以读取模式打开文件
fileptr1=fopen(“c:\\CTEMP\\Dictionary.txt”、“r”);
ch=getc(fileptr1);
while(ch!=EOF)
{
printf(“%c”,ch);
ch=getc(fileptr1);
}
//倒带
倒带(fileptr1);
printf(“\n输入要删除行的行号:”);
scanf(“%d”,删除行(&U);
//以写模式打开新文件
fileptr2=fopen(“replica.c”、“w”);
ch=getc(fileptr1);
while(ch!=EOF)
{
ch=getc(fileptr1);
如果(ch='\n')
{
temp++;
}
//要删除的行除外
如果(温度!=删除线)
{
//复制文件replica.c中的所有行
putc(ch,fileptr2);
}
}
fclose(fileptr1);
fclose(fileptr2);
删除(“c:\\CTEMP\\Dictionary.txt”);
//将文件replica.c重命名为原始名称
重命名(“replica.c”,“c:\\CTEMP\\Dictionary.txt”);
printf(“\n修改后的文件内容如下:\n”);
fileptr1=fopen(“c:\\CTEMP\\Dictionary.txt”、“r”);
ch=getc(fileptr1);
while(ch!=EOF)
{
printf(“%c”,ch);
ch=getc(fileptr1);
}
fclose(fileptr1);
扫描单位(“%d”);
返回0;
}

无法从文件中删除某些内容。文件系统不支持它

这就是您可以修改文件内容的方式:

  • 删除整个文件内容(无论何时打开文件进行写入,默认情况下都会删除)
  • 删除文件本身
  • 重写文件的某些部分,用相同的字节数替换几个字节
  • 附加到文件的末尾

因此,要删除一个单词,您应该将整个文件读入内存,删除该单词,然后重写它,或者用空格(或任何其他字符)替换该单词。

您不能在某个地方按下按钮,然后从文件的开头、中间或结尾删除某些内容。您必须读取文件的现有内容,并创建一个新文件,而不包含删除的部分。为什么要进行向下投票?在这个问题上我没有发现任何奇怪的地方。@SamVarshavchik是的,我知道。我已经看到有一个函数fgets(),可以应用它从文本文件中读取一行并将其存储到指向的字符串中。然而,我不知道我将如何应用它。因为我不想用任何东西替换字符串,因为我想删除它。@AlfonsoNishikawa我不知道为什么。。我在这里所做的就是像其他人一样寻求帮助,我认为这是一个有意义的问题。谢谢。免费提示:现在这一行你“想删除它”。但是它前后的每一行呢。您知道您可以使用
fgets()
阅读每一项。好的,那么您认为这些行会发生什么情况呢?谢谢您的回复,但我正在寻找更简单的方法。@JosuelSpiteri大多数文件编辑都是这样做的。你读了全部内容;存储在内存中;操纵它;然后再次将新内容传输到文件中。或者,您应该尝试使用SQL。它需要处理文件内容,但需要额外的库。这样做比suggested@user5821508:那是胡说八道。不能使用SQL操作文本文件。