删除C语言中的文本块

问题描述 投票:0回答:1

[请帮助我使用C程序从文本文件中删除文本块:例:文件内容:索引:1; NAME:姓名;规格:规格注意:笔记bla-bla-bla-----索引:2; NAME:姓名;规格:规格注意:笔记bla-bla-bla-----指数:3; NAME:姓名;规格:规格注意:笔记bla-bla-bla-----

在C程序中:输入索引:2

文件内容为:索引:1; NAME:姓名;规格:规格注意:笔记bla-bla-bla-----指数:3; NAME:姓名;规格:规格注意:笔记bla-bla-bla-----

我认为该算法是下一个:我读取了文件的内容。创建一个新文件。如果没有输入索引,则将内容从INDEX:n ...记录到-----到另一个文件。然后,在文件结束时,删除第一个文件,然后将第二个文件重命名为第一个文件。但我不知道该如何实现

我完成的代码:

    char *filename, *p, *index, *tmp, *q; //variables
    int id, number;
    filename = (char*)malloc((L_tmpnam + 1) * sizeof(char)); //
    p = (char*)malloc(L_tmpnam * sizeof(char));
    tmp = (char*)malloc(10 * sizeof(char));
    puts("Enter the index");
    scanf("%d", &id);

    tmpnam(filename);
    p = strstr(filename, "Temp\\"); p += 5;
    *(p + (strlen(p) - 2)) = '\0';
    strcat(p, ".txt");
    FILE *f, *f2; //for first and second files
    f = fopen("catalog.txt", "rt"); //opening for reading
    f2 = fopen(p, "wt"); //opening for writing
    while (fscanf(f, "INDEX: %d; NAME: %s; SPEC: %s", number, "name", "spec") != EOF) { //checking if index is that I need
        if (number == id) { //here has to be NOT recording the content
            while (strcmp(tmp, "-----") != 0) continue;
        }
        else { //Here has to be recording the content to another file
        }
    }
    fcloseall(); // closing all the files
c file text recording
1个回答
0
投票

要写入文件,您可以使用fputs,fwrite,fprintf之类的功能,具体取决于您要实现的目标以及所拥有的数据类型。

© www.soinside.com 2019 - 2024. All rights reserved.