This blog is under construction

Friday, 27 April 2012

fgets example in C

Header file:

    char *fgets(char *str, int n, FILE *stream);

     It reads n-1 character from the input stream or stops reading if a newline character is encountered.  And this newline is included in the input string.  '\0' is included as the last character in our input string.  String "str" is returned as the return value on success.  Otherwise, NULL is returned.

fgets function C example:

  int main() {
        char str[100], *ret;
        printf("Enter your string input:\n");
        ret = fgets(str, 90, stdin);
        str[strlen(str) - 1] = '\0';
        printf("Output:\nret: %s\nstr: %s\n", ret, str);
        return 0;

  jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
  Enter your string input:
  hello world
  ret: hello world
  str : hello world

No comments:

Post a Comment