0

I am trying to do duplicate elimination from clients.txt (which has 7 names and surnames, some of them are repeated). In the end of file it writes the output to output.dat file. I did not get any error during the compiling but when i try to run it, it gives "003.exe stopped working" error. (003.c is C project name)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct names
{
    char name[25];
    char surname[25];
};


int main()
{
    int i, j;
    char a[1] = {""};
    struct names name[200];
    FILE *file;
    FILE *file2;
    file = fopen("clients.txt", "r");
    if (ferror(file))
    {
        printf ("File could not be opened");
    }

    while (fscanf(file, "%s", a) == 2)
    {
        i = 0;
        fscanf(file, "%s %s", name[i].name, name[i].surname);
        i++;
    }
    for (i = 0; i < 200; i++)
    {
        for (j = 0; j < 200; j++)
        {
            if (i != j && strcmp(name[i].name, name[j].name) == 0 && strcmp(name[i].surname, name[j].surname) == 0 )
            {
                strcpy(name[j].name, a);
                strcpy(name[j].surname, a);
            }
        }
    }
    fclose(file);
    file2 = fopen("output.dat", "w");
    {
        for (i = 0; i < 200; i++)
        {
            if ( strcmp(name[i].name, "") == 1 )
            {
                fprintf(file, "%s %s\n", name[i].name, name[i].surname);
            }
        }
    }
    fclose(file2);

    system("pause");
    return 0;
}
Andro
  • 2,232
  • 1
  • 27
  • 40
Sabri Özgür
  • 632
  • 5
  • 10
  • 2
    Welcome to Stack Overflow. Please read the [About] page soon. Your `fgetc()` should really be replaced by `while (fscanf(file, "...", ...) == 2)`. You also use `free(name[j].name)` but you never use `malloc()`; that is unconditionally a bug. Your double-nested loop goes from 0..200 twice, even though only `i` entries were found. You will need to fix loop indexes (or use `int n = i;` and test against `n`). You need to think what you want to do when you find a duplicate. Logically, you want to move everything after the duplicate down one row. – Jonathan Leffler May 27 '14 at 20:33
  • I edited a bit. I switched `free()` with `strcpy()` so that it copies an empty string to those which are duplicated. For the double-nested loop, there i tried to compare one name data with all other name datas. If they match it frees the string. In the next loop i tried to print those which are not empty to the file. Now it compiles and runs fine but output.dat contains awkward symbols. – Sabri Özgür May 27 '14 at 21:15
  • how is this: `fscanf(file, "%s", a) == 2` *ever* going to be true? – WhozCraig May 27 '14 at 21:59

1 Answers1

0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct names {
    char name[25];
    char surname[25];
};

int isEqual(struct names *a, struct names *b){
    return strcmp(a->name, b->name) == 0 && strcmp(a->surname, b->surname)==0;
}

int main(){
    int i, j;
    struct names name[200], a;
    FILE *file;

    file = fopen("clients.txt", "r");
    if (!file){//ferror can't use to fopen
        printf ("File could not be opened");
        return -1;
    }

    i=0;
    while (fscanf(file, "%24s %24s", a.name, a.surname) == 2){
        int dup = 0;
        for(j=0; j < i ;++j){
            if(dup=isEqual(&a, &name[j]))
                break;
        }
        if(!dup)//!dup && i<200
            name[i++] = a;
    }
    fclose(file);

    file = fopen("output.dat", "w");
    for (j = 0; j < i; ++j){
        fprintf(file, "%s %s\n", name[j].name, name[j].surname);
    }
    fclose(file);

    system("pause");
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70