/*
 *  49.c:       A program to test successive 10 digit numbers  
 *              extracted from e with a summation of 49.      
 *  Author:     V. Alex Brennen (http://cryptnet.net/people/vab/)
 *  Date:       2004.07.22 
 *  Copyright:  Public Domain.
 */

#include 


int main(void)
{
        int answ = 0;
        int i = 3;
        FILE *in = NULL;
        char *candidate = NULL;
        char *tmp_buffer = NULL;

        candidate = (char *)malloc(11);
        if(candidate == NULL)
        {
                fprintf(stderr,"Malloc call failed.\n");

                return -1;
        }
        tmp_buffer = (char *)malloc(11);
        if(tmp_buffer == NULL)
        {
                fprintf(stderr,"Malloc call failed.\n");

                return -1;
        }
        if(NULL == (in = fopen("out.txt","r")))
        {
                fprintf(stderr,"Failed to open out file.\n");

                return -1;
        }

        while(!feof(in))
        {
                if(i)
                        fseek(in,i,SEEK_SET);
                fread(candidate,sizeof(char),10,in);
                candidate[11] = '\0';
                memcpy(tmp_buffer,candidate,11);
                answ = sum_digits(tmp_buffer);
                if(answ == 1)
                {
                        printf("%s\n",candidate);
                }
                i++;
        }
        fclose(in);
        if(candidate != NULL)
                free(candidate);
        if(tmp_buffer != NULL)
                free(tmp_buffer);

        return 0;
}

int sum_digits(char *candidate)
{
        unsigned int a = 0;
        unsigned int b = 0;
        unsigned int c = 0;
        unsigned int d = 0;
        unsigned int e = 0;
        unsigned int f = 0;
        unsigned int g = 0;
        unsigned int h = 0;
        unsigned int i = 0;
        unsigned int j = 0;
        unsigned int x = 0;


        if(candidate == NULL)
                return -1;
        if(strlen(candidate) != 10)
                return -1;      
        
        candidate[10] = '\0';
        a = atoi(&candidate[9]);
        candidate[9] = '\0';
        b = atoi(&candidate[8]);
        candidate[8] = '\0';
        c = atoi(&candidate[7]);
        candidate[7] = '\0';
        d = atoi(&candidate[6]);
        candidate[6] = '\0';
        e = atoi(&candidate[5]);
        candidate[5] = '\0';
        f = atoi(&candidate[4]);
        candidate[4] = '\0';
        g = atoi(&candidate[3]);
        candidate[3] = '\0';
        h = atoi(&candidate[2]);
        candidate[2] = '\0';
        i = atoi(&candidate[1]);
        candidate[1] = '\0';
        j = atoi(&candidate[0]);

        x = a+b+c+d+e+f+g+h+i+j;
        if(x == 49)
        {
                return 1;
        }

        return 0;
}