如何从文本文件中删除? [英] How can I delete from a text file?

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

问题描述

大家好,我有一个文本文件,称为dictionary.txt.基本上,我正在做两个选择的菜单,1.在字典中添加新单词,以及2.从字典中删除单词.现在我设法做菜单并添加新单词.但是,我一直坚持从文件中删除.我希望当用户输入"runners"时,在dictionary.txt中搜索该单词并将其删除.告诉您我在学校还没有涉及到的所有内容,但是我在这里正在寻找一些想法,以便我可以继续完成任务.我已经尝试了一些方法,但是正如我已经告诉您的那样,我还没有介绍它,所以我不知道该怎么做.我感谢所有的帮助.下面是我的程序.

Hi guys I have a text file which is known as dictionary.txt. Basically i am doing a menu of two choices, 1. add new words to dictionary and 2. delete words from dictionary. right now i managed to do the menu and add new words. However, I am stuck at deleting from a file. I want that when the user enters for example "runners" the word is searched in the dictionary.txt and it is deleted. To tell you everything I haven't covered it yet at school but I am looking for some ideas here so that I can move on with the task. I have tried some things but as I already told you, I haven't covered it yet so I don't know how to actually do it. I appreciate all the help. below is my program.

推荐答案

  • 您打开两个文件:一个文件(用于读取)和一个新文件(用于写入).
  • 您循环浏览第一个文件,依次读取每一行.
  • 您将每行的内容与需要删除的单词进行比较.
  • 如果该行与任何删除词都不匹配,则将其写入新文件.
  • 约瑟夫,摘自我先前由理查德·乌尔文(Richard Urwin)回答的问题

    Josuel, as taken from my previously answered question by 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;
    
        }
    

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

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