Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
fgets()和get()的使用 #包括 #包括 int main(){ char ch,文件名[25]; 文件*fp; printf(“输入您希望查看的文件名\n”); 获取(文件名); fp=fopen(文件名,“r”);//用于读取模式 如果(fp==NULL){ printf(stderr,“打开文件时出错。\n”); 返回(-1); } printf(“文件%s的内容是:\n”,文件名); 而((ch=fgetc(fp))!=EOF) printf(“%c”,ch); fclose(fp); 返回0; }_C_Fgets - Fatal编程技术网

fgets()和get()的使用 #包括 #包括 int main(){ char ch,文件名[25]; 文件*fp; printf(“输入您希望查看的文件名\n”); 获取(文件名); fp=fopen(文件名,“r”);//用于读取模式 如果(fp==NULL){ printf(stderr,“打开文件时出错。\n”); 返回(-1); } printf(“文件%s的内容是:\n”,文件名); 而((ch=fgetc(fp))!=EOF) printf(“%c”,ch); fclose(fp); 返回0; }

fgets()和get()的使用 #包括 #包括 int main(){ char ch,文件名[25]; 文件*fp; printf(“输入您希望查看的文件名\n”); 获取(文件名); fp=fopen(文件名,“r”);//用于读取模式 如果(fp==NULL){ printf(stderr,“打开文件时出错。\n”); 返回(-1); } printf(“文件%s的内容是:\n”,文件名); 而((ch=fgetc(fp))!=EOF) printf(“%c”,ch); fclose(fp); 返回0; },c,fgets,C,Fgets,这段代码似乎有效,但我一直收到一条警告:“警告:此程序使用gets(),这是不安全的。” 所以我尝试使用fgets(),但我得到了一个错误,它指出“参数太少,无法调用预期的函数3” 有办法解决这个问题吗?首先:永远不要使用get()。。它会导致缓冲区溢出 第二:向我们展示如何使用fgets()。。正确的方法应该如下所示: #include <stdlib.h> #include <stdio.h> int main() { char ch, file_name[

这段代码似乎有效,但我一直收到一条警告:“警告:此程序使用gets(),这是不安全的。”

所以我尝试使用fgets(),但我得到了一个错误,它指出“参数太少,无法调用预期的函数3”


有办法解决这个问题吗?

首先:永远不要使用
get()
。。它会导致缓冲区溢出

第二:向我们展示如何使用
fgets()
。。正确的方法应该如下所示:

#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;
}
供参考:

fgets
通过在末尾添加
\0
字符,将整个输入转换为字符串

如果有足够的空间,那么
fgets
也将从您的输入(stdin)中获取
\n
。。要删除
\n
并仍将整个输入作为字符串,请执行以下操作:

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

第一:永远不要使用
get()
。。它会导致缓冲区溢出

第二:向我们展示如何使用
fgets()
。。正确的方法应该如下所示:

#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;
}
供参考:

fgets
通过在末尾添加
\0
字符,将整个输入转换为字符串

如果有足够的空间,那么
fgets
也将从您的输入(stdin)中获取
\n
。。要删除
\n
并仍将整个输入作为字符串,请执行以下操作:

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

第一:永远不要使用
get()
。。它会导致缓冲区溢出

第二:向我们展示如何使用
fgets()
。。正确的方法应该如下所示:

#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;
}
供参考:

fgets
通过在末尾添加
\0
字符,将整个输入转换为字符串

如果有足够的空间,那么
fgets
也将从您的输入(stdin)中获取
\n
。。要删除
\n
并仍将整个输入作为字符串,请执行以下操作:

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

第一:永远不要使用
get()
。。它会导致缓冲区溢出

第二:向我们展示如何使用
fgets()
。。正确的方法应该如下所示:

#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;
}
供参考:

fgets
通过在末尾添加
\0
字符,将整个输入转换为字符串

如果有足够的空间,那么
fgets
也将从您的输入(stdin)中获取
\n
。。要删除
\n
并仍将整个输入作为字符串,请执行以下操作:

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
是:需要3个参数:缓冲区(与
get
相同)、缓冲区大小和要读取的流。在您的情况下,可以通过
sizeof file\u name
获得缓冲区大小,并且您要从中读取的流是
stdin
。总而言之,您可以这样称呼它:

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

   file_name[strlen(file_name)] = '\0';
gets
不安全的原因是它不知道(无法)将读入的缓冲区的大小。因此,它很容易发生缓冲区溢出,因为即使缓冲区已满,它也会继续写入缓冲区

fgets
没有这个问题,因为它会让您提供缓冲区的大小

ADDIT:如果(fp==NULL)对
中的
printf
的调用无效
printf
的第一个参数是格式,而不是输出流。我想你应该改打
fprintf

最后,为了正确检测
while
条件中的
EOF
,必须将
ch
声明为
int
EOF
不一定适合
char
,但它适合
int
(并且
getc
还返回
int
)。您仍然可以使用
%c
打印它。是:需要3个参数:缓冲区(与
获取
相同)、缓冲区大小和要读取的流。在您的情况下,可以通过
sizeof file\u name
获得缓冲区大小,并且您要从中读取的流是
stdin
。总而言之,您可以这样称呼它:

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

   file_name[strlen(file_name)] = '\0';
gets
不安全的原因是它不知道(无法)将读入的缓冲区的大小。因此,它很容易发生缓冲区溢出,因为即使缓冲区已满,它也会继续写入缓冲区

fgets
没有这个问题,因为它会让您提供缓冲区的大小

ADDIT:如果(fp==NULL)
中的
printf
的调用无效
printf
的第一个参数是格式,而不是输出流。我想你应该改打
fprintf

最后,为了正确检测
while
条件中的
EOF
,必须将
ch
声明为
int
EOF
不一定适合
char
,但它适合
int
(并且
getc
还返回
int
)。您仍然可以使用
%c
打印它。是:需要3个参数:缓冲区(与
获取
相同)、缓冲区大小和要读取的流。在您的情况下,可以通过
sizeof file\u name
获得缓冲区大小,并且您要从中读取的流是
stdin
。总而言之,您可以这样称呼它:

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

   file_name[strlen(file_name)] = '\0';
get
不安全的原因是它不(不能)知道将读入的缓冲区的大小