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


int main() {
   char ch, file_name[25];
   FILE *fp;

   printf("Enter the name of file you wish to see\n");
   gets(file_name);

   fp = fopen(file_name,"r"); // is for read mode

   if (fp == NULL) {
      printf(stderr, "There was an Error while opening the file.\n");
      return (-1);
   }

   printf("The contents of %s file are :\n", file_name);

   while ((ch = fgetc(fp)) != EOF)
         printf("%c",ch);

   fclose(fp);
   return 0;
}

This code seems to work but I keep getting a warning stating "warning: this program uses gets(), which is unsafe."

So I tried to use fgets() but I get an error which states "too few arguments to function call expected 3".

Is there a way around this?

4

3 回答 3

2

First : Never use gets() .. it can cause buffer overflows

second: show us how you used fgets() .. the correct way should look something like this:

fgets(file_name,sizeof(file_name),fp); // if fp has been opened
fgets(file_name,sizeof(file_name),stdin); // if you want to input the file name on the terminal

// argument 1 -> name of the array which will store the value
// argument 2 -> size of the input you want to take ( size of the input array to protect against buffer overflow )
// argument 3 -> input source

FYI:

fgets converts the whole input into a string by putting a \0 character at the end ..

If there was enough space then fgets will also get the \n from your input (stdin) .. to get rid of the \n and still make the whole input as a string , do this:

   fgets(file_name,sizeof(file_name),stdin);

   file_name[strlen(file_name)] = '\0';
于 2013-11-03T00:00:09.833 回答
1

Yes: fgets expects 3 arguments: the buffer (same as with gets), the size of the buffer and the stream to read from. In your case your buffer-size can be obtained with sizeof file_name and the stream you want to read from is stdin. All in all, this is how you'll call it:

fgets(file_name, sizeof file_name, stdin);

The reason gets is unsafe is because it doesn't (cannot) know the size of the buffer that it will read into. Therefore it is prone to buffer-overflows because it will just keep on writing to the buffer even though it's full.

fgets doesn't have this problem because it makes you provide the size of the buffer.

ADDIT: your call to printf inside the if( fp == NULL ) is invalid. printf expects as its first argument the format, not the output stream. I think you want to call fprintf instead.

Finally, in order to correctly detect EOF in your while-condition you must declare ch as an int. EOF may not necessarily fit into a char, but it will fit in an int (and getc also returns an int). You can still print it with %c.

于 2013-11-03T00:00:16.263 回答
0

Rather than ask how to use fgets() you should either use google, or look at the Unix/Linux man page or the VisualStudio documentation for the function. There are hundreds of functions in C, C++ and lots of class objects. You need to first figure out how to answer the basics yourself, so that your real questions stand a chance of being answered.

If you are new to C, you are definitely doing the right thing of experimenting, but take a look at other code, as you go along, to learn some of the tips/tricks of how code is written.

于 2014-02-08T18:09:26.473 回答