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 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;
}