如何根据C中的用户输入编辑文件的特定部分?

时间:2017-12-02 18:58:38

标签: c string file scanf fopen

我正在完成一项家庭作业,要求我创建一个制作字典的程序。我的大部分任务都已完成,但我仍处于最复杂的部分。我坚持的作业部分是:

  

编辑字典(E)

Insert a new word (I) (15 points)
    If the word is available, it should alert a message.
    Otherwise, insert the new word
Delete a word (D) (15 points)
    If the word is not available, it should alert a message.
    Otherwise, delete the word
Editing a word (U) (10 points)
    If the word is not available, it should alert a message

目前我正在使用的文件如下所示:

  
Name of product: Dictionary
Description: Spanish to English Dictionary
Developer: Robert
Date of Creation: 12/02/2017
Number of words: 0
***************************************************************************
1. Red Rojo
2. Cat Gato
3. Apple Manzana
4. Love Amor
5. Car Carro

我想知道如何阅读,并进行上述操作。谷歌的大部分内容对我目前的技能都非常复杂。这是我到目前为止的情况?有人ELI5可以在这种情况下做我应该做的一般过程吗?我应该使用结构吗?我怎么能按照教授要我的方式与文件进行交互?到目前为止,这是我对本节的基本代码:

int editDictionary(char *oDict) {
    char array[1000];
    char temp[100];
    FILE * dictPtr; // dictPtr = dictionary.dic file pointer

    /* fix directory for dictionaries */
    strcat(temp,"./dicts/");
    strcat(temp,oDict);
    strcpy(oDict,temp);

    /* open dictPtr file for rw */
    dictPtr = fopen (oDict,"a+");
    fscanf(dictPtr,"%s",array[1]);

    /* text scan values */
    for(int i = 0; i <5; i ++) printf("%s", array[i]);
    return 1;
}

1 个答案:

答案 0 :(得分:0)

您的任务非常繁琐,需要了解C中的一些内置函数。

因为这是你的家庭作业,所以提供整个代码对改善你现有的技能没有好处。因此,我将提供执行delete操作的代码,这是最棘手的字典。休息你必须弄清楚自己。 delete_dict中的代码足以帮助您编写其他函数。

我已在代码中添加了大多数使用的函数的解释,但您仍应查阅其文档以了解每个函数的作用。

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

const char *WORD_COUNT_LABEL = "Number of words: ";

void delete_word(const char *filename, const char *word)
{
    //add alerts
    char read[100];
    FILE *f = fopen(filename, "r");
    int exists = 0; //the count of words that exist
    //check if word exists
    //fgets is used to read a line into the buffer/temp memory read
    while (fgets(read, 100, f))
    {
        //strstr - finds substring
        if (strstr(read, word))
        {
            exists++;
        }
    }
    if (exists == 0)
    {
        fclose(f);
        return;
    }

    //The main objective to read the file two times is
    //because we want to edit the NUMBER_OF_WORDS in the file as well

    //now we know that a word exists
    //so move flie pointer to beginning
    fseek(f, 0, SEEK_SET);

    FILE *nf = fopen("dict_t.txt", "w");
    int count;
    char num[10];
    char *p;
    while (fgets(read, 100, f))
    {
        //if word count label is found update it
        //return the pointer to the location of match
        if (p = strstr(read, WORD_COUNT_LABEL))
        {
            p += strlen(WORD_COUNT_LABEL);
            //incrementing the pointer to the position where number is to be updated

            count = atoi(p); //converts integer from char * to int
            count -= exists;
            sprintf(num, "%d", count); //converts interger from int to char *
            strcpy(p, num);            //copy new number at that location
            strcat(p, "\n");
        }
        else if (p = strstr(read, word))
        {
            //if we find the word to be deleted
            //we set the first character in read as NULL
            //so that nothing is written onto the new file.
            read[0] = '\0';
        }
        //writes char * to file
        fputs(read, nf);
    }
    //rest is self explanatory
    fclose(nf);
    fclose(f);
    remove(filename);
    rename("dict_t.txt", filename);
}