Thread: Delete line when previous line is same

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    31

    Delete line when previous line is same

    Hello, I would like to read a text file and delete those lines that occur twice. The single values are separated with a semicolon. I parse the data and distinguish by date. If the current date is the same as the previous one, this line should not be written into the new file "output".

    Only at this point I don't know how to implement this in C. I currently try to skip this line with fgetc(fp)!='\n'; but this does not work.

    My question. What do I have to insert instead of this line
    Code:
    fgetc(fp)!='\n'
    in the code so that the duplicate line is not copied into the new file?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_LINE_LENGTH 1000      // maximum length of a line in the input file
    
    char lines[MAX_LINE_LENGTH];
    
    int main()
    {
        // Open the input file
        FILE *fp = fopen("input.txt", "r");
        if (fp == NULL)
        {
            printf("Failed to open the file.\n");
            return 1;
        }
    
            FILE *fpp = fopen("output.txt", "w");
        if (fp == NULL)
        {
            printf("Failed to open the file.\n");
            return 1;
        }
    
    
        int count =0;
        char ticker[50], dateActual[50], dateOld[50];
        char line[MAX_LINE_LENGTH];
    
        while ( (fgets(line, sizeof(line), fp) != NULL))
        {
    
            sscanf(line,"%[^;];%[^;];\n",ticker, dateActual);
    
    
            if(strcmp(dateActual,dateOld)==0)
            {
                fgetc(fp)!='\n';
            }
    
            fprintf(fpp, "line [%d]%s\n",count, line);
            memcpy(dateOld, dateActual, sizeof(dateActual));
    
            count++;
    
        }
    
    
        // Close the input and output file
        fclose(fp);
    
        return 0;
    }
    Last edited by jasmin89; 07-16-2023 at 06:13 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Just do
    Code:
    if ( strcmp(dateActual,dateOld) != 0 ) {
        // date is different, write it out.
        fprintf(fpp, "line [%d]%s\n",count, line);
    }
    You should also initialise dateOld to be a proper empty string to begin with.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    31
    Thanks! It worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 12-12-2012, 12:17 PM
  2. How to read previous line of file?
    By Programmer_P in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2012, 05:31 AM
  3. How to go to a previous line
    By Megamanenm in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2009, 06:49 PM
  4. Returning to previous line
    By Garland in forum C++ Programming
    Replies: 7
    Last Post: 01-12-2008, 06:15 AM
  5. going back to previous line
    By sankul in forum C Programming
    Replies: 5
    Last Post: 08-28-2007, 11:55 PM

Tags for this Thread