Little bit late, but you should check if the files are realy opened before you use it.
Code:
…
    FILE *fp = NULL, *outfile = NULL;

    if ((fp = fopen("be.txt", "r")) == NULL) {
        fprintf(stderr, "could not open file 'be.txt'! exit.\n");
        return 1;
    }
    if ((outfile = fopen("ki.txt", "w")) == NULL) {
        fprintf(stderr, "could not open file 'ki.txt'! exit.\n");
        fclose(fp);
        return 1;
    }
…
And dont forget to close it if you dont need it anymore.