Thread: fgets() from stdin and strcat()

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    84

    fgets() from stdin and strcat()

    hey , how do i append a string to input i have received from stdin.

    Code:
     
      printf("Please enter a word to search for:\n");
       fgets (input,MAX,stdin);
       check = strtok(input, "\n");
    i wanna do somethin like

    Code:
    input = strcat(input , " - ");
    it compiles fine but doesnt seem to append the two strings ?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    strcat appends a string to the argument you pass as destination.
    In your code, it would append " - " to input.
    But beware that strtok actually damages the original input string (it changes the input string you provide it), which might not be what you want...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    yes, thats what i want it to do , but its not .. i wanted input to be appended with " - " , but its not doing that .. do you mean its because of strtok ?..but thats being called after i append the string. is there any other way to append a string to the input ?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then you must be doing something "bad" in your code. Will you not show us what you are doing?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    sure... here it is..basically im trying to create an amateur dictionary and the words is the file are seperated from the meanings by a " - " , and in case a word is repeated , the function returns the first value it found for example if i search for car it will return alacarte which comes before car in the text file. unless i explicitly search for "car -"

    Code:
      FILE *dict;
       char buffer[MAX];
       char input[MAX];
       char *check;
       char *found;
    
    /*...*/
    
       printf("Please enter a word to search for:\n");
       fgets (input,MAX-1,stdin);
       input[strlen(input)+1] = strcat(input , " -");
       check = strtok(input, "\n");
    
        fgets (buffer, MAX-1, dict);
    	while (!strstr(buffer, check)) 
    	{
    		fgets (buffer, MAX-1, dict);
    		count++;
    		found = strstr(buffer,check);
    	}
    
    	if(found)
    	  printf( "\n%s\n", found );

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
       input[strlen(input)+1] = strcat(input , " -");
       check = strtok(input, "\n");
    I'd be very surprised if the first line of this section doesn't give you a warning (should the case be that you don't then you should turn up the warning level of the compiler), as you are assigning the result of strcat to a character in your string (which so happens to be the last character of the length of the string - whether that is BEFORE or after input has been appended is an interesting question, which I beleive the standard doesn't define (so you may have strlen() from before strcat, or from after strcat) - be that as it may, since you don't actually want to do this anyways. Just strcat(input, " -"); will be fine.

    The second line cuts the line off at the newline. Since the input from fgets() has a newline at the end, you'd get something like "abc\n", then append " -" -> a string holding "abc\n -", and then cut it at the strtok(), so you end up with "abc". I don't think that's what you wanted to do.

    Also:
    Code:
       fgets (input,MAX-1,stdin);
    If you are planning to append two characters to teh string, maybe you'd like to make that MAX-2, so that you are guaranteed to fit at least 2 more characters in the string.

    --
    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.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not to mention that strlen(input) + 1 is beyond the length of the string itself, and thus a dangerous thing, and also quite unnecessary.
    And mats also warns that the line may be undefined behavior since the standard may not guarantee which is done first, the strlen call or the strcat call, so you may get different results depending on which is done first, which may differ in different compilers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    Thank you matsp and Elysia! , i know the strlen function i used was wrong but before that i tried using simply , strcat(input , " -") and even tried assigning it to another char* but i cant seem to append anythin to input after i get the user input. is there a way to append " -" at the same time the user types his input into stdin, so that the value of input is "... -" , so that it doesnt get cut off by strtok() ?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
        char buffer[MAX];
        fgets(input, MAX - 2, stdin);
        strtok(input, "\n");
        strcat(input, " -");
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    Wow! it worked !that was easy..i feel stupid.. Thank You!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Teaching myself C, quick inquiry
    By Iconate in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 03:19 PM
  2. Pointer Manipulation with strcat()
    By radiohead in forum C Programming
    Replies: 3
    Last Post: 03-03-2006, 07:17 AM
  3. whats wrong with this
    By psycho88 in forum C Programming
    Replies: 2
    Last Post: 11-30-2005, 06:45 PM
  4. customer input
    By buckwheat88 in forum C Programming
    Replies: 6
    Last Post: 11-21-2005, 08:01 PM