标签: gets

"获取"的隐含声明

我理解"隐式声明"通常意味着在调用函数之前必须将函数置于程序的顶部,或者我需要声明原型.
但是,gets应该在stdio.h文件中(我已经包含在内).
有没有什么办法解决这一问题?

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

int main(void)
{
   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"); // read mode
   if( fp == NULL )
   {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
   }
}
Run Code Online (Sandbox Code Playgroud)

c linux gets

9
推荐指数
2
解决办法
3万
查看次数

在gets()中从Ruby中的TCP套接字恢复

我正在读取TCP套接字上的输入行,类似于:

class Bla  
  def getcmd
    @sock.gets unless @sock.closed?
  end

  def start     
    srv = TCPServer.new(5000)
    @sock = srv.accept
    while ! @sock.closed?
      ans = getcmd
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

如果端点在getline()运行时终止连接,则gets()挂起.

我该如何解决这个问题?是否有必要进行非阻塞或定时I/O?

ruby sockets tcp gets

7
推荐指数
2
解决办法
9112
查看次数

变得有用吗?

在我看来,人们,特别是在学习C编程语言时,仍然使用该gets函数从stdin读取数据.尽管它已经从C11标准中删除了1,并且关于cppreference的免责声明如下:

gets()函数不执行边界检查,因此该函数极易受到缓冲区溢出攻击.它不能安全使用(除非程序在限制stdin上可能出现的内容的环境中运行).因此,该功能已在C99标准的第三个更正中弃用,并在C11标准中完全删除.fgets()和gets_s()是推荐的替代品.

永远不要使用gets().

然而,似乎这不是一个提出更多现代编程哲学的新问题.它本来会被破坏并导致程序崩溃,我看不出"限制stdin上出现的内容的环境"可能意味着什么.

那么,它过去是否有用?或者它被添加到以前的标准和预标准版本的C的原因是什么?


(1) ......或至少更改为具有指示要读取的最大长度的附加参数.然而,我询问旧签名,只接收指针.

c history gets standard-library

7
推荐指数
2
解决办法
322
查看次数

是否有任何函数可以从标准输入中获取无限的输入字符串

条件是:

我想从标准输入中输入一行,我不知道它的大小,也许很长.

方法,例如scanf,gets需要知道您可以输入的最大长度,以便您的输入大小小于缓冲区大小.

那么有什么好方法可以解决它吗?

答案必须只在C 而不是C++中,所以c ++字符串不是我想要的.我想要的是C标准字符串,类似于char*结束'\0'.

c gets input scanf char

7
推荐指数
2
解决办法
5625
查看次数

用fgets()替换gets()

我一直在测试这个结构,我收到有关使用的警告gets.有人提到要使用fgets而是替换结尾'\0'.任何建议我如何更改我的代码来做到这一点?

void regCars(Car reg[], int *pNrOfCars) {
    char again[WORDLENGTH] = "yes", model[WORDLENGTH], tmp[WORDLENGTH];
    int year, milage;

    while (strcmp(again, "yes") == 0) {
        printf("Enter model:");
        gets(model);
        printf("Enter Year:");
        gets(tmp);
        year = atoi(tmp);
        printf("Enter milage:");
        gets(tmp);
        milage = atoi(tmp);
        reg[*pNrOfCars] = createCar(model, year, milage); 
        (*pNrOfCars)++;
        printf("Continue? (yes/no)");
        gets(again);
    }
}
Run Code Online (Sandbox Code Playgroud)

c gets fgets

7
推荐指数
1
解决办法
663
查看次数

通过头文件在GCC中禁用警告消息?

gets()在我的C代码中使用该函数.我的代码工作正常,但我收到一条警告信息

(.text+0xe6): warning: the `gets' function is dangerous and should not be used.
Run Code Online (Sandbox Code Playgroud)

我希望不会弹出此警告消息.有什么办法吗?

我想知道通过创建一个用于禁用某些警告的头文件可能存在这种可能性.或者在编译期间有任何选项可以满足我的目的吗?或者可能有一种特殊的方法gets()用于此警告不要弹出?

c gcc warnings gets header

6
推荐指数
6
解决办法
1万
查看次数

fgets()调用重定向获取异常数据流

我正打算用C语言编写一个shell.以下是源代码:

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <stdlib.h>

int
getcmd(char *buf, int nbuf)
{
  memset(buf, 0, nbuf);
  fgets(buf, nbuf, stdin);
  printf("pid: %d, ppid: %d\n", getpid(), getppid());
  printf("buf: %s", buf);
  if(buf[0] == 0) {// EOF
    printf("end of getcmd\n");
    return -1;
  }
  return 0;
}

int
main(void)
{
  static char buf[100];
  int fd, r, ret;

  // Read and run input commands.
  while((ret = getcmd(buf, sizeof(buf))) >= 0){
    if(fork() == 0)
      exit(0);
    wait(&r);
  }
  exit(0);
}
Run Code Online (Sandbox Code Playgroud)

当我执行编译后的可执行文件时,将stdin重定向到名为t.sh的文件,其内容为"1111 \n2222 \n",如./myshell <t.sh,输出为: …

c gets multiprocess

6
推荐指数
1
解决办法
139
查看次数

关于红宝石"得到"的问题

我想知道为什么当我试图获得不同的输入时它忽略了我的第二个输入.

#!/usr/bin/env ruby
#-----Class Definitions----

class Animal
  attr_accessor :type, :weight
end

class Dog < Animal
  attr_accessor :name
  def speak
    puts "Woof!"
  end
end

#-------------------------------

puts
puts "Hello World!"
puts

new_dog = Dog.new

print "What is the dog's new name? "
name = gets
puts

print "Would you like #{name} to speak? (y or n) "
speak_or_no = gets

while speak_or_no == 'y'
  puts
  puts new_dog.speak
  puts
  puts "Would you like #{name} to speak again? (y or n) "
  speak_or_no = …
Run Code Online (Sandbox Code Playgroud)

ruby gets input

5
推荐指数
1
解决办法
1万
查看次数

Ruby:带有默认值的gets.chomp

是否有一些简单的方法如何在提供默认值的同时在 Ruby 中请求用户输入?

考虑 bash 中的这段代码:

function ask_q {
    local PROMPT="$1"
    local DEF_V="$2"
    read -e -p "$PROMPT" -i "$DEF_V" REPLY
    echo $REPLY
}

TEST=$(ask_q "Are you hungry?" "Yes")
echo "Answer was \"$TEST\"."
Run Code Online (Sandbox Code Playgroud)

你能用 Ruby 实现类似的行为gets.chomp吗?

function ask_q(prompt, default="")
    puts prompt
    reply = gets.chomp() # ???
    return reply
def

reply = ask_q("Are you hungry?", "Yes")
Run Code Online (Sandbox Code Playgroud)

我知道我可以通过这种方式对 Ruby 中的功能进行排序复制......

def ask_q(prompt, default="")
    default_msg = (default.to_s.empty?) ? "" : "[default: \"#{default}\"]"
    puts "${prompt} ${default}"
    reply = gets.chomp()
    reply = (default.to_s.empty?) ? …
Run Code Online (Sandbox Code Playgroud)

ruby bash gets default-value

5
推荐指数
1
解决办法
1894
查看次数

fget 和 gets 之间的区别

fgets()和 和有什么区别gets()

当用户点击“输入”时,我试图打破循环。它与 配合得很好gets(),但我不想使用gets()。我尝试使用fgets()andscanf()但没有得到与使用相同的结果gets()fgets()无论用户在文本中输入什么,都会打破循环!这是我的代码:

void enter(void)
{
  int i,

  for(i=top; i<MAX; i++)
    {
      printf(".> Enter name (ENTER to quit): ");
      gets(cat[i].name);

      if(!*cat[i].name)
         break;

      printf(".> Enter Last Name: ");
      scanf("%s",cat[i].lastname);
      printf(".> Enter Phone Number: ");
      scanf("%s",cat[i].phonenum);
      printf(".> Enter e-Mail: ");
      scanf("%s",cat[i].info.mail);
      printf(".> Enter Address: ");
      scanf("%s",cat[i].info.address);
      printf("\n");
    }
  top = i;
}
Run Code Online (Sandbox Code Playgroud)

c gets scanf fgets

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

gets ×10

c ×7

ruby ×3

fgets ×2

input ×2

scanf ×2

bash ×1

char ×1

default-value ×1

gcc ×1

header ×1

history ×1

linux ×1

multiprocess ×1

sockets ×1

standard-library ×1

tcp ×1

warnings ×1