gets

Ruby 'gets' that works over multiple lines

試著忘記壹切 提交于 2019-12-01 15:25:06
Using the IRB, I want to enter a multiple line string in order to strip certain characters from it. "gets" only allows a single line - is there a similar function for multiple lines. ASCII_project.rb(main):002:0* puts = "What's the text you want to strip?" => "What's the text you want to strip?" ASCII_project.rb(main):003:0> str = gets I now want to paste in a section of text - because of the new lines it doesn't function. This is why I want to collect over multiple lines Here is the code # encoding: CP850 puts = "What's the text you want to strip?" str = gets str.gsub!(/\P{ASCII}/, '') puts

Ruby 'gets' that works over multiple lines

折月煮酒 提交于 2019-12-01 14:26:01
问题 Using the IRB, I want to enter a multiple line string in order to strip certain characters from it. "gets" only allows a single line - is there a similar function for multiple lines. ASCII_project.rb(main):002:0* puts = "What's the text you want to strip?" => "What's the text you want to strip?" ASCII_project.rb(main):003:0> str = gets I now want to paste in a section of text - because of the new lines it doesn't function. This is why I want to collect over multiple lines Here is the code #

gets() function and '\\0' zero byte in input

可紊 提交于 2019-12-01 12:41:24
Will the gets() function from C language (e.g. from glibc) stop, if it reads a zero byte ( '\0' ) from the file ? Quick test: echo -ne 'AB\0CDE' Thanks. PS this question arises from comments in this question: return to libc - problem PPS the gets function is dangerous, but it is a question about this function itself, not about should anybody use it or not. The behavior of gets() is that it stops when a newline character is encountered or if EOF is encountered. It does not care if it reads \0 bytes. C99 Standard, 7.19.7.7 Synopsis #include <stdio.h> char *gets(char *s); Description The gets

Sublime text can't understand gets.chomp

▼魔方 西西 提交于 2019-12-01 12:14:57
I wrote this simple program in ruby using Sublime Text and for some reason if I build it using Sublime text inbuilt system then I get the following error `deposit': undefined method `chomp' for nil:NilClass (NoMethodError) Its running perfectly if I run it using cmd. class BankAccount def initialize(name) @transactions = [] @balance = 0 end def deposit print "How much do you want to deposit?" amount = gets.chomp @balance += amount.to_f puts "$#{amount} is deposited" end def show_balance puts "Your balance is #{@balance}" end end bank_account = BankAccount.new("Rohit Begani") bank_account.class

Sublime text can't understand gets.chomp

半腔热情 提交于 2019-12-01 11:44:45
问题 I wrote this simple program in ruby using Sublime Text and for some reason if I build it using Sublime text inbuilt system then I get the following error `deposit': undefined method `chomp' for nil:NilClass (NoMethodError) Its running perfectly if I run it using cmd. class BankAccount def initialize(name) @transactions = [] @balance = 0 end def deposit print "How much do you want to deposit?" amount = gets.chomp @balance += amount.to_f puts "$#{amount} is deposited" end def show_balance puts

fgets() call with redirection get abnormal data stream

最后都变了- 提交于 2019-12-01 05:21:24
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. while((ret = getcmd(buf, sizeof(buf))) >= 0){ if(fork() == 0) exit(0); wait(&r); } exit(0); } When I

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

∥☆過路亽.° 提交于 2019-11-30 22:15:00
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? Why there are no references about it in the scanf man page? Why gcc does not warn when compiling this

Recovering from a broken TCP socket in Ruby when in gets()

半世苍凉 提交于 2019-11-30 20:20:35
I'm reading lines of input on a TCP socket, similar to this: 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 If the endpoint terminates the connection while getline() is running then gets() hangs. How can I work around this? Is it necessary to do non-blocking or timed I/O? You can use select to see whether you can safely gets from the socket, see following implementation of a TCPServer using this technique. require 'socket' host, port = 'localhost', 7000 TCPServer.open(host, port) do

gets.chomp without moving to a new line

余生长醉 提交于 2019-11-30 18:50:51
问题 I understand about the \n that's automatically at the end of puts and gets , and how to deal with those, but is there a way to keep the display point (the 'cursor position', if you will) from moving to a new line after hitting enter for input with gets ? e.g. print 'Hello, my name is ' a = gets.chomp print ', what's your name?' would end up looking like Hello, my name is Jeremiah, what's your name? 回答1: You can do this by using the (very poorly documented) getch : require 'io/console' require

Is the gets() string function in C considered a bad practice? [duplicate]

白昼怎懂夜的黑 提交于 2019-11-29 17:12:09
This question already has an answer here: Why is the gets function so dangerous that it should not be used? 11 answers was reading the Head first C book and stumbled across the author saying gets() to be a bad practice gets() is a function that’s been around for a long time. But all you really need to know is that you really shouldn’t use it. why is it considered a bad practice? Consider #include<stdio.h> int main() { char buffer[100]; gets(buffer); printf("The input is %s",buffer); } When user types input of length within 99 then there is no problem. But when user types more than 99