Я пытаюсь удалить дубликаты из данных, считанных из файла и сохраненных в массиве struct, и распечатать новые уникальные значения в файл
У меня есть файл со значениями x, y и индексом, iЯ прочитал данные из файла и сохранил их в массиве struct. Я хочу просмотреть массив и удалить дубликаты элементов (совпадение по x и y), наконец напечатать новые уникальные значения в файл. Файл, который я читаю, имеет нижеданные
- 12 20
- 20 29
- 68 87
- 20 29
Моя цель - прочитатьфайл в массиве struct, удалите дубликаты и распечатайте их в другой файл. Ниже приведен мой код
int store_fixation_point(FILE *fp,int id,struct fixation_point_type fixation_point[],int x, int y);
struct fixation_point_type {
int id_number;
int x;
int y;
};
int main()
{
int N_list,i,N,j;
int temp1 = 0;
float temp2, temp3;
int id_N0=0;
FILE *fp_in, *fp_out;
int fixation_number = 0, x =0, y=0;
char ch;
fixation_point_type fixationPoint [MAX_NUMBER_OF_POINTS];
fp_in = fopen("../data/input.txt", "r");
if (fp_in == NULL)
{
printf("Cannot open the input file \n");
exit(0);
}
fp_out = fopen("../data/output.txt", "w");
if (fp_out == NULL)
{
printf("Cannot open the output file \n");
exit(0);
}
fscanf(fp_in,"%d",&N_list);
i = 0;
do
{
i++;
N = 0;
temp2 = 0;
temp3 = 0;
do
{
if (fscanf(fp_in, "%d %f %f ", &temp1,&temp2,&temp3) != EOF){
if (temp2 == -1 && temp2 == -1)
{
break;
}
else
{
fixationPoint[N].id_number = temp1;
fixationPoint[N].x = temp2;
fixationPoint[N].y = temp3;
N++;
}
}
}while(1);
store_fixation_point(fp_out,N,fixationPoint,x,y);
fscanf(fp_in, "%d %d %d", &fixation_number, &x, &y);
N++;
for (j=0;j<N;j++)
{
printf("%3d %d %d\n",fixationPoint[j].id_number,(int)fixationPoint[j].x,(int)fixationPoint[j].y);
fprintf(fp_out,"%3d %d %d\n",fixationPoint[j].id_number,(int)fixationPoint[j].x,(int)fixationPoint[j].y);
}
printf("------------------------\n");
fprintf(fp_out,"------------------------\n");
}while(i<N_list);
fclose(fp_in);
fclose(fp_out);
return 0;
}
//function to remove duplicates
int store_fixation_point(FILE *fp,int indexID,struct fixation_point_type fixation_point[],int x, int y)
{
int i;
int a =0;
int index=0;
fixation_point_type temp[1000];
if(indexID == 0){
printf("%d %d %d\n",fixation_point[indexID].id_number, fixation_point[indexID].x,fixation_point[indexID].y);
}
else
{
for (i =0; i < indexID; i++){
if ( (fixation_point[i].x ==x) && (fixation_point[i].y==y) ){
a=a+1;
}
}
if (a==0)
{
printf("%d %d %d\n",fixation_point[indexID].id_number, fixation_point[indexID].x,fixation_point[indexID].y);
fprintf(fp,"%d %d %d \n",fixation_point[indexID].id_number, fixation_point[indexID].x,fixation_point[indexID].y);
}
return indexID;
Я ожидаю вывод уникальных значений, например
- 12 20
- 20 29
- 68 87