atoi 和 strtol 产生错误的结果
atoi and strtol produce erroneous results
#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;
}
我如何将两位数(char* 类型)转换为 int?
我想将值从标记转换为整数值
atoi 和 strtol 产生错误结果
输出是
815102162524#622101830754
token = 815102162524
token = 622101830754
815102162524, 622101830754
2147483647, 2147483647
-941623716, -668427166
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);
}
输出
Long long 64 bits
token = #
815102162524, 622101830754
#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;
}
我如何将两位数(char* 类型)转换为 int?
我想将值从标记转换为整数值 atoi 和 strtol 产生错误结果
输出是
815102162524#622101830754
token = 815102162524
token = 622101830754
815102162524, 622101830754
2147483647, 2147483647
-941623716, -668427166
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);
}
输出
Long long 64 bits
token = #
815102162524, 622101830754