我试图用gets()函数输入数据,但是每当程序执行到了lien时gets,它就会忽略它.
当我gets()没有先前的数据输入使用时,它运行正常.但是当我在数据输入后使用它时会发生问题.
这是在之前的数据输入之后使用它的代码(所以在执行中我不能将数据输入到字符串):
int main() {
char str[255];
int a = 0;
cin >> a;
if(a == 1) {
gets(str);
cout << "\n" << str << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么能解决这个问题?
注意:cin.getline也是如此
我今天注意到有时当我使用gets函数时,我的编译器只是忽略它.好.这是获取有效的示例:
#include <stdio.h>
void main()
{
char s[50];
gets(s);
puts(s);
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我对程序进行这个简单的更改,则会忽略函数gets:
#include <stdio.h>
void main()
{
int n;
printf("dati n:\n");
scanf("%d",&n);
char s[50];
gets(s);
puts(s);
}
Run Code Online (Sandbox Code Playgroud)
"忽略"意味着当我运行程序时,编译器会读取变量,然后退出而不读取我的字符串.为什么会这样?谢谢.
所以基本上我在学习C,他们教我们使用gets()函数,我发现该函数已被删除并且无法再使用。因此,我正在尝试学习使用替代方法gets_s()函数,但似乎无法使用。例如,以下代码在尝试编译时为我提供了“对gets_s的未定义引用”:
char line[21];
gets_s( line, 20 );
printf( "The line entered was: %s\n", line );
Run Code Online (Sandbox Code Playgroud)
很多人已经说过使用fgets()而不是gets(),但是我不明白它是如何工作的,因为我想读取用户的输入,而不是从文件中读取。
我看到了关于代码所使用的C的另一个问题gets(),我评论了关于从不使用的常见警告,gets()除非您想要演示如何破坏安全性.
这一次,我决定检查我的编译器是否发出了关于使用的警告gets().当然我希望它会.对,对吧?即使您没有指定任何警告?
想象一下,我的惊讶,当我发现,不仅编译器不会 默认警告,但我甚至不能弄清楚如何使之警告!
有问题的编译器是关于Debian的gcc 4.7.2,这是我使用的代码:
#include <stdio.h>
int main(void)
{
char s[10];
gets(s);
puts(s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
尝试gcc g.c.编译时没有警告.运行.如果输入太多文本,则获取段错误.
试过我通常放在makefile中的所有标准警告:
gcc -W -Wall -Wno-long-long -Wshadow -Wlarger-than-1000 \
-Wpointer-arith -Wbad-function-cast -Wcast-qual -Wcast-align \
-Wconversion -Waggregate-return -Wmissing-prototypes \
-Wmissing-declarations -Wpadded -Wredundant-decls -Wnested-externs g.c
Run Code Online (Sandbox Code Playgroud)
结果相同.
试过-std=c11.考虑gets()到C11中甚至不存在,即使这样也没有产生警告,这非常奇怪.也试过
c99.没有警告.
那么这里发生了什么?当我在整个C语言中使用最弃用的函数时,为什么这个使用非常广泛的编译器不会警告我?
编辑:根据Keith Thompson的建议,我检查了一个弃用属性stdio.h.它不存在.然后我复制了头文件并进行了实验.将这些字符串(我在其他标题中找到)添加到声明的末尾会产生警告:
__attribute_deprecated__
__attribute__ ((__deprecated__))
Run Code Online (Sandbox Code Playgroud)
警告:
‘gets’ is deprecated (declared at /usr/include/stdiotz.h:632) [-Wdeprecated-declarations]
Run Code Online (Sandbox Code Playgroud)
总结一下我到目前为止看到的响应,似乎我系统上的libc版本不包含警告,这在以后的版本中确实存在.这很奇怪,因为这个警告至少从1996年开始以某种形式存在.我模糊地回忆起libc已经至少分叉了一次,所以也许警告被遗漏在一个分支之外的时间明显晚于其他分支.
我想我会在Debian邮件列表上询问这个问题,并根据我学到的内容将其报告为bug. …
我开始学习C语言编程,这是我所指代的代码,其中显示了一些源代码,gets()而我的IDLE也认识到它。但是仍然在编译时,我的编译器不同意它。
谁能帮我吗?我gets()在main函数中使用clang作为编译器。
任何人都可以告诉我为什么get(abc)与char []一起使用但不与int一起工作?
int abc;
char name[] = "lolrofl";
printf("Hello %s.\n",name);
printf("\n >> ");
fflush(stdin);
gets (abc);
printf("\n die zahl ist %i.\n",abc);
system("Pause");
return(0);
Run Code Online (Sandbox Code Playgroud) 我试图用来gets()从用户那里获取一个字符串,但程序似乎正在通过gets()。用户输入时没有停顿。为什么gets()什么都不做?
char name[13];
printf("Profile name: ");
gets(name);
printf("\n%s", name);
Run Code Online (Sandbox Code Playgroud) 好的,这是在终端上运行的Dungeon Text Adventure项目中.
当用户说他想"向北"时,它会拆分字符串:第一个字检查方法,第二个字检查参数.这发生在其他地方,我复制粘贴了2个方法,给我以下问题:
当它调用go(北)并且north不是有效连接时,它显示选项并再次询问用户方向,用户在那一刻输入的输入由于某种原因存储为Nil.
为什么??我也尝试过STDIN.gets.chomp.downcase!并有相同的Nil结果
这是代码:
def find_room_in_direction(direction)
##-> if room connects with other on that direction, returns connection
if find_room_in_dungeon(@player.location).connections.include?(direction.to_sym)
return find_room_in_dungeon(@player.location).connections[direction.to_sym]
##-> if direction connection is not found,
##-> show possible connections & return trigger to ask again inside go()
elsif !find_room_in_dungeon(@player.location).connections.include?(direction.to_sym)
puts "I don't see any #{direction}..."
puts "This room only connects #{(find_room_in_dungeon(@player.location)).connections.keys.join(', ')}"
puts "Where should we go?"
return :redo
end
end
def go(direction)
current_direction = @player.location
new_direction = find_room_in_direction(direction)
##-> if REDO trigger received, …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
int length;
char **lines, buffer[80];
printf("How many lines do you want to enter?.\n");
scanf("%d", &length);
getchar();
lines = (char**)malloc(length * sizeof(char*));
for (i = 0; i < length; ++i) { //Scan user's lines
gets(buffer);
lines[i] = (char*)malloc(strlen(buffer) * sizeof(char) + 1);
if (!lines[i]) { //Check if allocation available
printf("Error! Out of memory!");
return endProgram;
}
strcpy(lines[i], buffer);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么gets()函数给我一个关于假设extern返回int的错误,我只是想扫描一个字符串.
我正在编写一个程序,允许用户输入五个名称并按字母顺序对名称进行排序,两个相邻名称由换行符分隔。这是我的代码:
void sortWords(char s[][100], int n){
int i, j;
char *str;
for(i = 0; i < n-1; i++){
for(j = n- 1; j > i; j--){
if(strcmp(s[j], s[j-1]) == -1){
strcpy(str, s[j]);
strcpy(s[j], s[j-1]);
strcpy(s[j-1], str);
}
}
}
}
int main(){
char s[5][100];
int i;
for(i = 0; i < 5; i++){
fflush(stdin);
//gets(s[i]); // when I use this statement, my program doesn't work
scanf("%s", s[i]);
}
sortWords(s, 5);
for(i = 0; i < 5; i++){
printf("%s ", s[i]); …Run Code Online (Sandbox Code Playgroud)