Delete A Specific Line From A File | C Programming Example

Поделиться
HTML-код
  • Опубликовано: 2 фев 2025

Комментарии • 44

  • @fraios1178
    @fraios1178 3 года назад +3

    I am a beginner in C, I try to watch some videos on how can I read a specific line but I cannot find a single video about it, But with your video I gain some an idea on how can I achieve it. thanks!

    • @PortfolioCourses
      @PortfolioCourses  3 года назад +2

      Maybe we’ll make a video on reading a specific line from a file next!

    • @fraios1178
      @fraios1178 3 года назад

      @@PortfolioCourses Wow that's great I definitely wait for it. thanks again have a great day!

    • @PortfolioCourses
      @PortfolioCourses  3 года назад +3

      @@fraios1178 Here you go: ruclips.net/video/w0mgn6OLKUs/видео.html

  • @Clouddistrictsnob
    @Clouddistrictsnob 2 года назад +1

    I'm new to C, but this method sounds like its pretty heavy, workload wise.
    Is there not a more efficient way to delete a particular line? You mentioned something about it in the beginning of your video.
    I know you can move to a specific location in the file and overwrite that particular spot, but that only changes the values in question, not free space. Is there a way to go about it or do I have to get all the data out of the file no matter what?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      If know the lengths of each lines in terms of the number of characters, we could speed this up for sure by skipping ahead a certain amount of characters based on the line number. And I suspect there is a way to speed up the writing of the remainder of the file past the deleted line, perhaps by no longer reading the file one line at a time from that point onward. I would need to play around with it more and think about it more to give a better answer. :-)

  • @bsykesbeats
    @bsykesbeats 2 года назад

    I'm so confused, the only way I can get this code to not also delete the last line is if I rewrite the do while loop like this:
    do
    {
    // Scan line
    fgets(buffer, MAX_LINE, file);
    if (feof(file)) {
    keep_reading = false;
    }
    else if (current_line == delete_line) {
    current_line++;
    continue;
    }
    fputs(buffer, tmp);
    current_line++;
    } while(keep_reading);
    I don't understand why the above would work any differently than the way you wrote it.

    • @bsykesbeats
      @bsykesbeats 2 года назад

      OHHHH I GET IT! When you write it this way:
      do
      {
      // Scan line
      fgets(buffer, MAX_LINE, file);
      if (feof(file)) keep_reading = false;
      else if (current_line != delete_line)
      fputs(buffer, tmp);
      current_line++;
      } while(keep_reading);
      When it reads the last line into the buffer, the file stream is now at EOF, so if (feof(file)) keep_reading = false; executes, and of course does not execute the else if statement, therefore the last line never gets written to the tmp file. So you would have to do it either the way I wrote in the above comment or just separate if and else if into two separate if statements. I'm still confused why it worked for you though....???

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      My version of the code is posted here: github.com/portfoliocourses/c-example-code/blob/main/delete_line.c. Maybe that will help? The big difference between my version and your first version is that in mine the writing of the line occurs in the else if case with fgets(), and my else if case has a != not an ==. Your 2nd version seems to do this as well. It should work because it will only write the line to the file if we haven't reached the end of the file *and* it's not the line we're trying to delete.

    • @bsykesbeats
      @bsykesbeats 2 года назад

      @@PortfolioCourses I appreciate the response! I think there might be some implementation dependent behavior in this code tho. I copied and pasted the code from your github into 3 different IDEs and it works on 1, but has the same bug on the other 2. (It works fine on VS Code installed locally on my Mac, but does not work on ubuntu cloud based IDEs...i.e. cs50 IDE and VS Code through Codespaces)... Do you see what I mean though as far as why I think it makes sense there's a bug? (When it reads the last line of code, the file stream is then at EOF, so the "if (feof(file))" part executes on the last line of code and therefore never executes the else if part which would write the last line to the file.)...But then if that's the case, I'm confused why it doesn't always have that bug on all platforms....Either way, great video, you're the only one who had a video on this! And again, thanks for the responses!

  • @vatsalyaa.m
    @vatsalyaa.m 2 года назад +1

    How to read the file ?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      Great question Vatsalya! 🙂It depends on how you want to read it... these videos might be helpful.
      Read A File Into An Array Of Strings: ruclips.net/video/X-1qodkHCHo/видео.html
      Read All Contents Of A File Into A String: ruclips.net/video/CzAgM5bez-g/видео.html
      Read A Specific Line From A File Into A String: ruclips.net/video/w0mgn6OLKUs/видео.html
      File Access Basics: ruclips.net/video/HQNsriyMhtY/видео.html

    • @vatsalyaa.m
      @vatsalyaa.m 2 года назад

      @@PortfolioCourses Oh sorry, i didn't mean that...
      My query was my compiler is not able to read my file

    • @vatsalyaa.m
      @vatsalyaa.m 2 года назад

      @@PortfolioCourses my code is correct but it is not working

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      I don’t know why, it’s hard to know as there could be multiple reasons. Is it the compiler that’s not reading the file? In other words does the compiler give an error of some kind? Or is it the program that can’t read the file after it’s been compiler? Because if it’s the program that can’t read the file the most likely cause is that the file is in the wrong directory.

    • @vatsalyaa.m
      @vatsalyaa.m 2 года назад

      @@PortfolioCourses yes i guess my file is in the wrong directory

  • @bsykesbeats
    @bsykesbeats 2 года назад

    Is there a way to do this with \b?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      I don't think we would use \b to do this... my memory is that \b does not play nicely with text files. :-)

    • @bsykesbeats
      @bsykesbeats 2 года назад

      @@PortfolioCourses Ahhh makes sense, yes /b was working for me when using printf(), but not fprintf(). Thanks :)

  • @Randomname6133
    @Randomname6133 3 года назад

    source code works like a charm, but I have a problem.
    temporary file not being renamed. I have made your code into a function and set filename into an argument. filename when called, filename was correct. opened. and read.

    • @PortfolioCourses
      @PortfolioCourses  3 года назад

      Can you post your code here or in a link to a GitHub repo? Maybe myself or someone else can take a look at it to help you out. :-)

    • @Randomname6133
      @Randomname6133 3 года назад

      @@PortfolioCourses honestly underrated channel, content provided is gold

    • @Randomname6133
      @Randomname6133 3 года назад

      @@PortfolioCourses one of the comments here mentioned they had problems with new lines for their file, I initially faced that problem as well but resolved the issue by simple adding
      on the line I append my data.hence file input will be
      1(emptyline)
      2 one two three
      3(emptyline)
      4four five six
      5(emptyline)

    • @Randomname6133
      @Randomname6133 3 года назад

      @@PortfolioCourses Also, can you make a video on how to replace a specific word from the specific line from a file

    • @Randomname6133
      @Randomname6133 3 года назад

      @@PortfolioCourses i checked your rename() video, and used errno, it says error 13, what do i do?

  • @nathanaelkamkamdem9906
    @nathanaelkamkamdem9906 Год назад

    Thanks you very much. It is really working

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      You’re very welcome Nathanael, I’m glad it’s working for you! :-)

  • @emirhandemir3872
    @emirhandemir3872 3 года назад

    Hello. I tried your code but there is a problem. When ı try to delete a line for the first time , it deletes the last line also. After doing this procces 1 time , second time do not delete the new file's last line. What could be wrong ?
    FILE *fp,*fpp;
    char file_name[SIZE];
    char temp_file_name[SIZE];
    char buffer[SIZE];
    int delete_line=0;
    printf("WHAT IS THE NAME OF YOUR FILE : ");
    scanf("%s",file_name);
    strcpy(temp_file_name,"temp____");
    strcat(temp_file_name,file_name);
    printf("WHICH LINE YOU WANT TO DELETE : ");
    scanf("%d",&delete_line);
    fp = fopen(file_name,"r");
    fpp = fopen(temp_file_name,"w");
    if(fp=='
    ' || fpp=='
    '){
    printf("ERROR OPENING FILES
    ");
    return 1;
    }
    bool keep_reading=true;
    int current_line=1;
    do{
    fgets(buffer,SIZE,fp);
    if(feof(fp))
    keep_reading=false;
    else if(current_line!=delete_line)
    fputs(buffer,fpp);
    current_line++;
    }
    while(keep_reading);
    fclose(fp);
    fclose(fpp);
    int a = remove(file_name);
    if(a==0)
    {
    printf("COPY PROGRES IS COMPLETED !");
    }

    • @PortfolioCourses
      @PortfolioCourses  3 года назад

      The code is posted here: github.com/portfoliocourses/c-example-code/blob/main/delete_line.c. When I try the code, it works. I am able to run the program to delete the last line, and then run the program again to delete the last line again (this time I need to use a different line number that is one higher than last time though).
      A couple of things I noticed with your code:
      - You are checking if the file pointers equal '
      ' to detect an error, I think you need to check if they equal NULL.
      - I see that you call remove() to remove the original file, but I don't see where you call rename() to rename the new file back to the original filename. Without this step, you may not be able to delete another line from the file the second time, because the original file no longer exists (but the new temporary file should still exist).

    • @emirhandemir3872
      @emirhandemir3872 3 года назад

      @@PortfolioCourses You had explained but ı will ask just to be sure. Does fgets() function read untill it finds a new line character - '
      ' - or only the size we expressed as parameter. What am tring to say, if it is going to skip to new line when it reaches the size we expressed as parameter or it will reading until new line character ? By the way thanks for explanation. I will consider them :)

    • @PortfolioCourses
      @PortfolioCourses  3 года назад +1

      @@emirhandemir3872 It will stop on "whichever comes first", either a newline, or that number of characters: www.tutorialspoint.com/c_standard_library/c_function_fgets.htm

    • @bsykesbeats
      @bsykesbeats 2 года назад

      I'm getting the same bug. I even copied and pasted the code from github, recompiled and I'm still getting that same bug where it deletes the last line in addition to the delete line.

  • @zahratemmar2851
    @zahratemmar2851 3 года назад

    thank you this was really helpful

  • @prabhuteja5110
    @prabhuteja5110 2 года назад

    I need code without using temporary file broo but its helpful

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      I'm glad to hear it was still helpful Prabhu! :-) For solving this problem without a temporary file, one thing you could do is read all the file contents into an array of strings, like in this video: ruclips.net/video/X-1qodkHCHo/видео.html. Then you could close the file, open it again for writing, and then have a loop write all the strings in the array back to the file, EXCEPT for the one with the line number you want to delete (which should be at the index "line_number - 1" in the array). Hopefully this helps! Maybe one day I can make a video where we delete a line in the file without using a temporary file.