fscanf keeps返回整数的错误值



我正在尝试访问一个名为'movies.dat'的文件,其中包含电影名称,总收入和发行年份。

目前我的程序将检索标题和年份正确。但是,总收入只有在我试图将其作为int检索时才会返回错误的值。如果值小于2150000000,它也会正确返回它。我尝试将其作为一个long检索,但返回相同的问题。

movies.dat

Gone_with_the_Wind 3713000000 1939
Avatar 3263000000 2009
Titanic 3087000000 1997
Star_Wars 3049000000 1977
Avengers:_Endgame 2798000000 2019
The_Sound_of_Music 2554000000 1965
E.T._the_Extra-Terrestrial 2493000000 1982
The_Ten_Commandments 2361000000 1956
Doctor_Zhivago 2238000000 1965
Star_Wars:_The_Force_Awakens 2206000000 2015
Snow_White 2150000000 1937
Jurassic_Park 2100000000 1993
Jaws 2100000000 1975
Avengers:_Infinity_War 2050000000 2018
The_Exorcist 2000000000 1973

Gone_with_the_Wind -581967296 1939
Avatar -1031967296 2009
Titanic -1207967296 1997
Star_Wars -1245967296 1977
Avengers:_Endgame -1496967296 2019
The_Sound_of_Music -1740967296 1965
E.T._the_Extra-Terrestrial -1801967296 1982
The_Ten_Commandments -1933967296 1956
Doctor_Zhivago -2056967296 1965
Star_Wars:_The_Force_Awakens -2088967296 2015
Snow_White -2144967296 1937
Jurassic_Park 2100000000 1993
Jaws 2100000000 1975
Avengers:_Infinity_War 2050000000 2018
The_Exorcist 2000000000 1973

#include<stdio.h>
#include<assert.h>
int main(void)
{
//Open File and check it opened correctly
FILE * fp;
fp = fopen("movies.dat", "r");
assert("Error: Could not open file" && (fp != NULL));
char text[256] = "00";
int gross = 0;
int year = 0;
//loop
while(!feof(fp))
{
//basic attempt
fscanf(fp, "%s %d %d", text, &gross, &year);
printf("%s %d %dn", text, gross, year);
}
//Close File
fclose(fp);
}

2150000000几乎等于231−1,即int为32位时INT_MAX的值。试着读long long中的大数字。

最新更新