How can I read a long integer with 12 or 13 digits (like a ISBN number of a book) in C? I want to read the number from a text file with information of books(ISBN/name/writer).
The content of the text file is like this:
0393312836
A Clockwork Orange
Anthony Burgess
0199536759
Middlemarch
Bret Easton Ellis
…
…
…
and I am using this code:
int main(void){
FILE *f;
char name[MAX], writer[MAX], line[MAX];
long isbn;
f=fopen("path.txt","r");
if(f == NULL){
return 0;
}
while (fgets(line, 1024, f) != NULL){
sscanf(line,"%ld", &isbn);
printf("ISBN: %ld\n",isbn);
fgets(nome, 1024, f);
printf("NAME: %s",name);
fgets(line, 1024, f);
printf("WRITER: %s",writer);
}
fclose(f);
return 0;
}
he is able to read the names of the books and the writers, but he only reads the numbers if they have 9 digits or less. what do I have to do to make this work?
I think for an ISBN you would be much better using a string. You won’t need to perform arithmetic on the value, you can store leading zeroes and you’ll want a string to store the X that you can get in an ISBN 10 checksum.