Hi guys,
what size of array should I use to get a double into it using fgets();
Thank you
Hi guys,
what size of array should I use to get a double into it using fgets();
Thank you
BUFSIZ would be my suggestion.
But then again, I would probably use BUFSIZ for reading ALL input, then validate whatever was typed in, just to minimise the problems of the user being a moron and typing excessively long lines of rubbish just to see if they can break your code.
For a double, you need about 15 decimal digits to fill the precision available.
myprog < myprog
is an extreme test of the input handling capabilities of your program
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Just bear in mind that floating point numbers can be expressed sub-optimally, e.g. using extra zero's at either end of the number, and easily reach into the 50's or 100's of digits and still be valid input for a double.
And if you do use a buffer that is shorter than bufsiz, at least make sure that the line you read through fgets() is complete - if it's not a complete line, ask the user to enter again [after clearing the buffer as per the FAQ]. Otherwise, you may find that you read something like this:
Feed that withCode:char buffer[20]; double d; int x; fgets(buffer, sizeof(buffer, stdin); d = strtod(buffer, ...); fgets(buffer, sizeof(buffer, stdin); i = strtol(buffer, ...); printf("i=%d, d = %f\n", i, d)
0.00000000000000000012345
4711
and you see the ouput of
--Code:0.00000 12345
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
a double is 8 bytes. so -
would do.Code:unsigned char Buffer[8];
> a double is 8 bytes. so
Which as nothing at all to do with it's representation as a string.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Sorry guys what is BUFSIZ?
BUFSIZ is a standard constant for "the buffer inside a FILE struct".
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Got it,
This is my stdio.h macro for BUFSIZ
I have few questions about it..Code:#if defined(_M_MPPC) #define BUFSIZ 4096 #else /* defined (_M_MPPC) */ #define BUFSIZ 512 #endif /* defined (_M_MPPC) */
1. Why should I use BUFSIZ ?
2. Is BUFSIZ is stdin buffer size too ?
3. Why BUFSIZ can be 4096 or 512 ?
4. If I'm writing a buffer[20000] to a file using fputs() will it take 20000/FILESIZ times to write a single fputs ?
Many thanks
Yes, a fputs() of a large block will take size/BUFSIZ number of actual write operations - if you don't want that behaviour, you need to use fwrite() or a lower level write operation - it is normally not a problem.
In the header file you are showing, it seems that PowerPC has a 4KB memory buffer, other architectures have 512 byte buffer. Why? Who knows - perhaps something in the PowerPC architecture makes it important that the buffer is 4KB...
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
> 1. Why should I use BUFSIZ ?
It exists to be used.
It may also signify to the reader that the buffer is only temporary and that any data read into the buffer is also temporary, and needs to be validated before being moved to the correct place in memory.
It also saves programmer decision time in trying to decide just how big to make the buffer. Excessive squeezing of a few bytes as in abachler's example leads to all sorts of other side effects like forgetting to count the newline AND the \0.
A couple of dozen "magic number" sizes all through the code may leave you wondering what's going on and what's safe to change. A consistent size may give you more confidence.
> Is BUFSIZ is stdin buffer size too ?
Yes, there's no problem using BUFSIZ for stdin.
Remember, stdin may be redirected from a file on some systems.
> Why BUFSIZ can be 4096 or 512 ?
It depends on the architecture (in your example). The system implementers have decided that on a PowerPC a size of 4096 is a better answer than 512.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.