我的輸入檔案如下所示:
Harry potter
9403133410 // his ID (this number changes to different one)
這是代碼:
void get(){
FILE* file;
int i = 0;
char load[50];
long x;
char *nac;
file = fopen("konferencny_zoznam.txt", "r");
while (fgets(load, sizeof load, subor) != NULL){
if (i == 0){
printf("Prezenter: %s", load);
}
if (i == 1){
x = strtol(load, &nac, 15); //trying to change 9403133410 to int, but it gives me different value
printf("%ld", x);
printf("Rodne Cislo: %s", load);
}
if (i == 2){
printf("Kod prezentacnej miestnosti: %s", load);
}
if (i == 3){
printf("Nazov prispevku: %s", load);
}
if (i == 4){
printf("Mena autorov: %s", load);
}
if (i == 5){
printf("Typ prezentovania: %s", load);
}
if (i == 6){
printf("Cas prezentovania: %s", load);
}
if (i == 7){
printf("Datum: %s", load);
}
if (i == 8){
printf("\n");
}
i ;
if (i == 9){i = 0;}
}
}
我需要該整數來創建錯誤,例如 ID 不能被 2 或 6 整除。
uj5u.com熱心網友回復:
x = strtol(load, &nac, 15);
首先,這是讀取以 15 為基數的數字。假設您實際上想要以 10 為基數,這應該是:
x = strtol(load, &nac, 10);
接下來,9403133410b10 太大而無法放入 32 位整數。 long不能保證比那個大(在 Windows 上不是),所以你應該long long改用。您還需要打電話strtoll閱讀它并使用%lld它來列印它。
long long x;
...
x = strtoll(load, &nac, 10);
printf("%lld", x);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/348672.html
標籤:C
