C 中的长整型
如何在 C 中读取 12 或 13 位长整数(如书籍的 ISBN 编号)?我想从包含书籍信息的文本文件中读取编号(ISBN/名称/作者)。
文本文件的内容是这样的:
0393312836
发条橙
安东尼·伯吉斯
0199536759
米德尔马奇
布雷特·伊斯顿·埃利斯
...
...
...
我正在使用这个代码:
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;
}
他能够读取书籍和作者的名称,但他只能读取 9 位或更少的数字。我需要做什么才能使这项工作成功?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为对于 ISBN,使用字符串会更好。您不需要对值执行算术运算,您可以存储前导零,并且您需要一个字符串来存储可以在 ISBN 10 校验和中获得的 X。
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.
只需将其读取为字符串(
char
数组)并如此对待即可。使用int
,您还会丢失开头的重要零以及有限范围。 ISBN 号码、电话号码等最好将其视为字符串,因为它们不代表实数(在数学意义上),而只是标识符。Just read it as string (
char
array) and treat it so. With anint
you also lose the important zeros at the start along with the limited range. ISBN numbers, phone numbers and the like are better treated as strings, because they don't represent real numbers (in a mathematical sense), but are just identifiers.尝试使用
unsigned long long
和%llu
说明符。在大多数平台上,前者应该是 64 位数字。您也可以尝试将其作为字符串读取。这取决于您以后想要将其作为字符串还是数字进行操作。
例如,如果您想按字母顺序对它们进行排序,请将它们作为字符串读取。如果您想将它们作为数字排序,请将它们视为数字。
Try using an
unsigned long long
and the%llu
specifier. The former should be a 64-bit number on most platforms.You could as well try reading it as a string. It depends on whether you want to manipulate it as a string or as a number later.
For example, if you want to sort them alphabetically, read them as strings. If you want to sort them as numbers, treat them as numbers.
最好将它们作为字符串或字符数组读取。 ISBN 编号是数字,意思是您将对其进行计算。相反,它们更像是一个恰好是数字的引用字符串。读取字符数组将让您获得包含破折号或其他分隔符的 ISBN 编号,如下所述: http://www.isbn.org/standards/home/isbn/international/html/usm4.htm
如果您确实想将它们存储为数字,则应该将其读入char 数组并清理它,以防输入中存在空格、破折号或其他非数字字符。
It might be best to read them in as Strings or char arrays. ISBN numbers are numbers in the sense that you'll be doing calculations on them. Instead, they are more like a reference string that just so happens to be numeric. Reading in a character array will let you get ISBN numbers that contain dashes or other separators as stated here: http://www.isbn.org/standards/home/isbn/international/html/usm4.htm
If you truly want to store them as numbers, you should read it in as a char array and clean it up in case there are spaces, dashes or other non-numeric characters in the input.