If you have
char buffer[20];
The max you can have is
scanf( "%19s", buffer );
Unlike fgets(), making scanf() respect the current size of the buffer is damn hard work.
If you have
char buffer[20];
The max you can have is
scanf( "%19s", buffer );
Unlike fgets(), making scanf() respect the current size of the buffer is damn hard work.
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.
Hi im getting an error for the following. Can someone tell me whats wrong:
Code:char name[20]="User\0"; bool nameLength = false; bool nameCase = false; do { fprintf(output,"%s please enter your name (20 max): ",name); fflush(output); fscanf(input,"%s",name); if (sizeof(name)<19) { nameCase = true; nameLength = true; } else { nameLength = false; } } while(nameLength == false);
Last edited by taurus; 09-22-2007 at 02:57 AM.
ok fixed.
edit: actually not, see when i enter more than 20 character it says segmentation fault and doesnt perform my do-while loop?
Last edited by taurus; 09-22-2007 at 02:57 AM. Reason: edit
> see when i enter more than 20 character it says segmentation fault
Haven't we explained this enough times already?
Also, sizeof() is not the same as strlen()
For any given variable or type, sizeof() will always give you the same answer, so using it to find out how much input there was is just broken.
And the segfault is all down to buffer overflow.
Stop messing about with fscanf to try and read a potentially long string into a small space and wondering why it goes pear shaped when it trashes memory.
Use fgets(), breath in, breath out and relax.
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.
yea but i tried replacing my fscanf with fgets and i get errors. Is there something i got to do else like importing a library?
There is one thing that you have to understand.
If you have a buffer with a certain size and you input more data that this buffer can hold then the damage is already done. There is no way to correct that problem aferwards.
You have to make shure before doing the input that no bufferoverflow will happen.
fgets will do just that. If you tell it the right size of the buffer it will not overwrite its bounds.
But still you will have to take special care for the case that the user inputs more data then the buffer can hold ( e.g. by checking if the last char in the returned string was '\n' ), otherwise the access input will stay in the inputbuffer and will screw up your following inputs.
The same is true when you use fscanf with a size argument in the format string. It's just that it is a lot harder to detect the case where not all the data was read from the input buffer.
Kurt
hmmm ok i see, but isint \n meaning 'enter'? so how would i do that co check
Guess there is plenty of that in the faq's.
You might want to try something like this
KurtCode:#include <stdlib.h> #include <stdio.h> #include <string.h> int main() { char buffer[10]; int done = 0; char * p; while ( !done ) { fgets( buffer, 10, stdin ); p = strchr(buffer,'\n'); if ( p != 0 ) { /* was all input read ? */ *p= 0; /* replace '\n' with string terminator */ printf("%s\n",buffer); done = 1; } else { /* just print 9 chars, fgets appends '\0' */ printf("%s",buffer); } } }
also got a problem when entering something in the char name field. if i enter 'bob the builder' it wont display anything after the bob, so after the space nothing?
Another reason to use fgets.
Kurt
so do i basically replace fscanf with fgets? simply change the word
No, of course not. fscanf requires a parse string, fgets does not. fscanf takes the FILE* as the first parameter, fgets as the last. Look in your C reference for fgets, the correct way to call it is given there.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law