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

int main() {
    char *filename = "Sayilar.txt";
    FILE *fp = fopen("D:\\WWW\\Yazilim\\cLionProject\\untitled3\\Sayilar.txt", "r");

    if (fp == NULL)
    {
        printf("Error: could not open file %s", filename);
        return 1;
    }

    // reading line by line, max 256 bytes
    const unsigned MAX_LENGTH = 256;
    char buffer[MAX_LENGTH];

    while (fgets(buffer, MAX_LENGTH, fp))
        printf("%s\n", buffer);

    // close the file
    fclose(fp);

    char *token;
    char *dizi[] = {"1","2"};
    long int dizi2[] = {1, 2};
    int num1, num2;

    token = strtok(buffer, "#");
    printf("token = %s\n", token);
    //num1 = strtol(token, NULL, 10);
    num1 = atoi(token);
    dizi[0] = token;
    dizi2[0] = strtol(dizi[0], NULL, 10);


    token = strtok(NULL, "#");
    printf("token = %s\n", token);
    //num2 = strtol(token, NULL, 10);
    num2 = atoi(token);
    dizi[1] = token;
    dizi2[1] = strtol(dizi[1], NULL, 10);

    printf("%s, %s\n",dizi[0], dizi[1]);
    printf("%ld, %ld\n",dizi2[0], dizi2[1]);
    printf("%d, %d",num1, num2);


    return 0;
}

How would I go about converting a two-digit number (type char*) to an int?

I want to convert the value from token to integer value atoi and strtol produce erroneous results

output is

815102162524#622101830754

token = 815102162524

token = 622101830754

815102162524, 622101830754

2147483647, 2147483647

-941623716, -668427166
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • 4
    These numbers are too big for being `int`. – Eugene Sh. Aug 03 '21 at 14:32
  • 2
    `atoi()` has no error checking builtin; `strtol()` does. Use `strtol()` and check for errors! `errno = 0; foo = strtol(text, &error_checking_pointer, 10); /* check errno, check error_checking_pointer */` – pmg Aug 03 '21 at 14:33
  • ı tried long int, same problem – Hami KESKİN Aug 03 '21 at 14:34
  • Pretty sure the problem is not the same. `atoi` is converting to `int`, use `strtol`. – Eugene Sh. Aug 03 '21 at 14:35
  • [fits with factors of room to spare](https://godbolt.org/z/Peha33Y64). Note that I'm not error checking `strtol` here, you can find out more info about how to do that on its [man page](https://man7.org/linux/man-pages/man3/strtol.3.html) and by googling around. You're saying "same problem", I suspect you're not using the correct `printf` format specifier to print a `long` (it's `"%ld"`). – yano Aug 03 '21 at 14:42
  • 1
    Does your input file really contain numbers like 815102162524 and 622101830754? Those are really big numbers. Depending on your processor and compilation model, they might not even fit in `long int`. They're 40-bit numbers. You might need `long long int`, and `strtoll()`. – Steve Summit Aug 03 '21 at 14:47
  • The reason you got different results for `atoi` versus `strtol` is that `strtol` is has well-defined behavior on overflow and other errors, while `atoi` does not. Since 815102162524 doesn't fit in a `long int` on your machine, `strtol` gave you the largest number it could, as it's supposed to: 2147483647. `atoi`, on the other hand, did something crazy. (It essentially took 815102162524 % 4294967296, giving 3353343580, which when interpreted by `%d` as a 2's complement signed integer gave -941623716.) – Steve Summit Aug 03 '21 at 14:52
  • Clearly neither `815102162524` nor `622101830754` are "_a two-digit number_". They both have 12 digits. – Clifford Aug 03 '21 at 15:57

1 Answers1

2
int main(void) {
    char buffer[] = "815102162524#622101830754\n";
    long long int num1, num2;
    char *p;
    printf("Long long %u bits\n",sizeof(num1)*8);

    num1 = strtoll(buffer, &p, 10);
    printf("token = %c\n", *p);
    num2 = strtoll(p+1,NULL,10);

    printf("%lld, %lld\n",num1,num2);
}

Output

Long long 64 bits
token = #
815102162524, 622101830754
stark
  • 12,615
  • 3
  • 33
  • 50