I'm new into programming. While doing a project to my university I've encountered a problem. My goal is to make a program which will be summing big numbers (more than 20 chars, less then 100). They should be writen in the .txt file divided with space. The problem is that after splitting those numbers I can only get the first one; the second one is changed into some strange characters (đv1). I'd be really grateful if somebody can help me with this.
int il_w = 4;
char * split(int w, int k)
{
char const* const fileName = "Numbers.txt";
FILE* file = fopen(fileName, "r");
char linia[256];
char * pch = '\0';
int i = 0;
for(i = 0; i < w + 1; i++)
fgets(linia, sizeof(linia), file);
i = 0;
pch = strtok (linia, " ");
while (i < k)
{
pch = strtok (NULL, " ");
i++;
}
fclose(file);
return pch;
}
int compare(char * a, char * b)
{
printf("%s ", a);
printf("%s", b);
//a > b -> 0, a < b -> 1, a = b -> 2
if(strlen(a) > strlen(b))
return 0;
else if(strlen(a) < strlen(b))
return 1;
else
{
int i = 0;
for(i = 0; i < strlen(a); i++)
{
int anum = atoi(a[i]);
int bnum = atoi(b[i]);
if (anum > bnum)
return 0;
else if (anum < bnum)
return 1;
else
continue;
}
return 2;
}
}
int main()
{
int i = 0;
while(i < il_w)
{
char * number1 = split(i, 0);
printf("%s \n", number1);
char * number2 = strtok(split(i, 1), "\n");
printf("%s \n", number2);
printf("%d \n", compare(number1, number2));
i++;
}
return 0;
}