gets

How to use “gets” function in C++ after previous input?

删除回忆录丶 提交于 2019-12-22 14:52:32
问题 I tried to input data with gets() function, but whenever program execution get to the the lien with the gets , it ignores it. When I use gets() without previous data input, it runs properly. But when I use it after data input the problem happens. Here's the code where it is used after previous data input (so in execution I can't input data to string): int main() { char str[255]; int a = 0; cin >> a; if(a == 1) { gets(str); cout << "\n" << str << endl; } } How could I fix this? NB: the same

c : gets() and fputs() are dangerous functions?

南笙酒味 提交于 2019-12-22 00:25:07
问题 In the computer lab at school we wrote a program using fputs and the compiler returned an error gets is a dangerous function to use and a similar error for fputs but at home when i type in this bit of code: #include <stdio.h> main() { FILE *fp; char name[20]; fp = fopen("name.txt","w"); gets(name); fputs(name,fp); fclose(fp); } i get no errors what so ever. The one at school was similar to this one, just a bit lengthy and having more variables. I use codeblocks at home and the default gcc

Question about “gets” in ruby [duplicate]

微笑、不失礼 提交于 2019-12-19 08:04:23
问题 This question already has answers here : Ruby: String Comparison Issues (5 answers) Closed 2 months ago . I was wondering why when I'm trying to gets to different inputs that it ignores the second input that I had. #!/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? "

Question about “gets” in ruby [duplicate]

99封情书 提交于 2019-12-19 08:03:51
问题 This question already has answers here : Ruby: String Comparison Issues (5 answers) Closed 2 months ago . I was wondering why when I'm trying to gets to different inputs that it ignores the second input that I had. #!/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? "

fgets() call with redirection get abnormal data stream

别等时光非礼了梦想. 提交于 2019-12-19 07:43:04
问题 I was about to write a shell with C language. Here is the source code below: #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.

fgets() call with redirection get abnormal data stream

时光怂恿深爱的人放手 提交于 2019-12-19 07:42:35
问题 I was about to write a shell with C language. Here is the source code below: #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.

if one complains about gets(), why not do the same with scanf(“%s”,…)?

▼魔方 西西 提交于 2019-12-19 03:14:17
问题 From man gets : Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead. Almost everywhere I see scanf being used in a way that should have the same problem (buffer overflow/buffer overrun): scanf("%s",string) . This problem exists in this case?

scanf(“%[^\n]s”,a) vs gets(a)

非 Y 不嫁゛ 提交于 2019-12-18 10:29:51
问题 I have been told that scanf should not be used when user inputs a string. Instead, go for gets() by most of the experts and also the users on StackOverflow. I never asked it on StackOverflow why one should not use scanf over gets for strings. This is not the actual question but answer to this question is greatly appreciated. Now coming to the actual question. I came across this type of code - scanf("%[^\n]s",a); This reads a string until user inputs a new line character, considering the white

Why does gets(stdin) return an integer? And other errors [duplicate]

廉价感情. 提交于 2019-12-18 08:57:24
问题 This question already has answers here : What is going on with 'gets(stdin)' on the site coderbyte? (3 answers) Closed 4 months ago . I am new to C programming (although I have experience with Java). After reading some tutorials, I decided to start solving coding challenges on Coderbyte. The first challenge I tried was this one: Challenge Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it. For example: if num = 4, then your program should

Why is gets throwing an error when arguments are passed to my ruby script?

一个人想着一个人 提交于 2019-12-17 19:13:58
问题 I'm using gets to pause my script's output until the user hits the enter key. If I don't pass any arguments to my script then it works fine. However, if I pass any arguments to my script then gets dies with the following error: ruby main.rb -i main.rb:74:in `gets': No such file or directory - -i (Errno::ENOENT) from main.rb:74:in `gets' ... The error message is showing the argument I passed to the script. Why would gets be looking at ARGV? I'm using OptionParser to parse my command line