Thread: length of char array

  1. #31
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    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.

  2. #32
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    error??

    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.

  3. #33
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    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

  4. #34
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    > 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.

  5. #35
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    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?

  6. #36
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by taurus View Post
    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

  7. #37
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    hmmm ok i see, but isint \n meaning 'enter'? so how would i do that co check

  8. #38
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Guess there is plenty of that in the faq's.
    You might want to try something like this
    Code:
    #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);
            }
        }
    }
    Kurt

  9. #39
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    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?

  10. #40
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Another reason to use fgets.
    Kurt

  11. #41
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    so do i basically replace fscanf with fgets? simply change the word

  12. #42
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game and strcmp
    By crazygopedder in forum C Programming
    Replies: 12
    Last Post: 11-23-2008, 06:13 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM