-3

The following program is used to write a string to a file When I compile using gcc it shows the errors

#include<stdio.h>
#include<string.h>
#include <stdlib.h>

int main() {
    FILE *fp;
    char s[80];
    fp = fopen("POEM.TXT", "w");
    if(fp == NULL) {
        puts("Cannaot open file");
        exit(1);
    }
    printf("\n Enter");
    while(strlen(gets(s)) > 0) {
        fputs(s, fp);
        fputs("\n", fp);
    }
    fclose(fp);
    return 0;
}

the error when i am compiling is

gcc expi.c
expi.c: In function ‘main’:
expi.c:18:14: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
 while(strlen(gets(s))>0)
              ^
expi.c:18:14: warning: passing argument 1 of ‘strlen’ makes pointer from integer without a cast [-Wint-conversion]
In file included from expi.c:2:0:
/usr/include/string.h:394:15: note: expected ‘const char *’ but argument is of type ‘int’
 extern size_t strlen (const char *__s)
               ^
/tmp/ccHMKvW7.o: In function `main':
expi.c:(.text+0x87): warning: the `gets' function is dangerous and should not be used.

the code is not compiling it is text book code and i am not being able to run it . it creates a file but it doesnt add runtime text to it

5
  • @PaulRooney: Warnings in C are often like errors in other languages. They should be resolved. gets is not part of the C standard (since C11, deprecated since C99) for good reasons. Commented Sep 13, 2016 at 0:52
  • @PaulRooney: That's wrong! Due to the missing declaration, C falls back to the legacy function type, which includes potentially problematic coercions. So trying to run a code you get warnings for is in general a bad idea. Interestingly even the linker warns about this function. Commented Sep 13, 2016 at 0:59
  • Please get a better book. Your compiler tells you quite clearly that gets is bad. Commented Sep 13, 2016 at 12:09
  • @qarma should i delete my post Commented Sep 15, 2016 at 13:03
  • Yeah, that's reasonable to remove the question. The gets part of the question is exact duplicate of another. Commented Sep 19, 2016 at 9:38

1 Answer 1

0

First of all, DO NOT USE gets(). It's a very dangerous function because there is a huge risk of overflowing the array and also, it was removed from recent standard .

Also, you should understand how strlen() works and how are represented. You, can do exactly the same as

while (strlen(string) > 0)

by just writing

while (string[0] != '\0')

but for that you need to understand what a is. And you should check that string is not a NULL pointer in both cases.

Maybe this is what you want

while (fgets(s, sizeof(s), stdin) != NULL) ...

not that fgets() is basically the functionality of gets() implemented in such a way that it can be safe avoiding buffer overflow.

Sign up to request clarification or add additional context in comments.

1 Comment

gets has been deprecated in the previous standard (C99). It has been removed from the standard with C11. (afaik it is the only function which has been removed).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.