fprintf misunderstandings and unhandled exception

0

Here a piece of code:

long long someNumber;
...

do{
    ...
    scanf("%d", &someNumber);
    ...
} while (...);
fprintf(someFile, "%d", someNumber);

I need to read a number which contains 11 chars. I've tried all scanf() and fprintf() modes like "%Ld", "%lld", etc. but the result always is not what I printed. But it is not even my problem. Then VS gives me an error when I'm typing ENTER: "Unhandled exception at 0x54A0F365 (msvcr120d.dll) in proj2.2.exe: 0xC0000005: Access violation reading location 0x0000000A." It opens "output.c" and refers to the line:

while ((ch = *format++) != _T('\0') && charsout >= 0) {

Actually, it prints a wrong number into file. But an error doesn't allow to continue the work of the program. What is happenening ? Where is my mistake ?

c
visual-studio
visual-studio-2013
asked on Stack Overflow Apr 9, 2015 by Nikolay Tynyanov • edited Apr 9, 2015 by Mark B

2 Answers

0

The format for reading a long long is "%lld". Use

scanf("%lld", &someNumber);

Use the same format for writing it out too.

fprintf(someFile, "%lld", someNumber);
answered on Stack Overflow Apr 9, 2015 by R Sahu
0

Per the docs, the correct data type specifier for type long long is ll. You need to use this both with scanf() and with printf():

long long someNumber;

scanf("%lld", &someNumber);
printf("%lld", someNumber);

If fixing the field specifier as above does not resolve your issue, then you have an additional problem somewhere else in your code.

answered on Stack Overflow Apr 9, 2015 by John Bollinger

User contributions licensed under CC BY-SA 3.0