Thread: How to Search and delete line from a text file in c?

  1. #1
    Registered User
    Join Date
    Nov 2021

    Question How to Search and delete line from a text file in c?


    I am writing a really basic database using a text file in c. It is supposed to ask the user, what they want to do. If they select add a user, it add the details to the text file created. If they select to delete, it should delete that user. If they select to search, they should be able to search the text file. At last, if they select 4 the program will close.

    I managed to get the first part working. But I really do not know, how to search or delete from a text file?

    What should I do? I am a beginner, so with the most basic commands, if possible.

    Thank you very much.

    My code so far:

    #include <stdio.h>
    typedef struct hraci
        char meno [40];
        char priezvisko [49];
        char vek[5];
        char vaha[30];
        char vyska[10];
    } HRACI;
    int main ()
        FILE *fb;
        int menu;
        int id_hraca = 1;
    HRACI hraci [100];
            printf("Stlac 1 ak chces pridat noveho hraca\n");
            printf("Stlac 2 ak chces vymazat hraca\n");
            printf("Stlac 3 ak chces zmenit udaje hraca\n");
            printf("Stlac 4 ak chces ukoncit program\n");
            scanf ("%d",&menu);
            if (menu==1)
            fb = fopen("hraci.txt","a+");
            printf("Zadaj meno a priezvisko\n");
            printf("Zadaj vek hraca\n");
            printf("Zadaj hmotnost hraca\n");
            printf("Zadaj vysku hraca\n");
            fprintf(fb,"Meno: %s",hraci[id_hraca].meno);
            fprintf(fb,"Vek: %s",hraci[id_hraca].vek);
            fprintf(fb,"Vaha :%s",hraci[id_hraca].vaha);
            fprintf(fb,"Vyska: %s",hraci[id_hraca].vyska);
            system ('cls');

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    A couple of basic things first before trying more advanced things.

    > fgets(hraci[id_hraca].meno,50,stdin);
    Don't lie about your buffer sizes.

    Use sizeof on the array you're reading into, like so.
    > system ('cls');
    Your compiler should have warned you about wide character constants or something.
    Watch your use of single (') vs double (") quotes.
    You need to use "" for strings.

    You should be thinking about how to make each of your menu choices into separate functions.
    Otherwise your main is going to be 100's of lines long and very difficult to manage.

    > fprintf(fb,"Meno: %s",hraci[id_hraca].meno);
    It would be a really good idea if your file format was the same as your input.

    void readRecord(FILE *stream, HRACI *rec) {
        // etc
    Then when you want to read from the user, you do
    readRecord(&hraci[id_hraca], stdin);
    And when you want to read from the file...
    readRecord(&hraci[id_hraca], fb);
    Create a similar writeRecord() function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-11-2011, 10:32 PM
  2. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  3. Replies: 8
    Last Post: 12-09-2008, 12:07 PM
  4. delete line from text file
    By Waldo2k2 in forum Linux Programming
    Replies: 6
    Last Post: 05-07-2005, 06:03 PM
  5. How to delete every other line in a text file?
    By mr2999 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2005, 06:08 PM

Tags for this Thread