gets

Why and how gcc emits warning for gets()?

守給你的承諾、 提交于 2019-12-02 15:06:05
while(1) { printf("\nEnter message : "); gets(message); //Send some data if( send(sock , message , strlen(message) , 0) < 0) { puts("Send failed"); return 1; } //Receive a reply from the server if( recv(sock , server_reply , 2000 , 0) < 0) { puts("recv failed"); break; } puts("Server reply :"); puts(server_reply); } close(sock); return 0; } This is part of my program. When I compile and run it, I get an error. The error's message is warning: the gets function is dangerous and should not be used! Shreevardhan A simple google search would have given a lot of useful information like this answer.

Ruby gets() not returning correct string [duplicate]

≯℡__Kan透↙ 提交于 2019-12-02 14:03:38
问题 This question already has answers here : Ruby: String Comparison Issues (5 answers) Closed last month . I decided to give Ruby a go today after hearing all of the great things about it, but so far it has only been giving me a hard time. A long time ago I made a "search engine" while learning Python that just stores data in an array and checks if the search keyword is in it, and I've been trying to do the same thing in Ruby. Although it wasn't as intuitive as it was in Python, I got the search

Is it possible using gets without knowing the length of the array in c?

假如想象 提交于 2019-12-02 14:01:29
If we want to use gets in c we will do something like: int main(void) { char str[100]; while (gets(str)) { printf("%s\n",str); } } We have to know the length of str first(which is 100) and then use gets. Is it possible using gets without knowing the length of the array in c? If you mean using gets safely, no, it's not possible. Advice : don't use gets , because without knowing the length first, it may cause buffer overflow. Use fgets instead, or use gets_s in C11. In fact, gets has been removed from stdio.h since C11. (In C99, it's deprecated) Short answer: No. Long answer: If you know that

Strings, gets and do while

筅森魡賤 提交于 2019-12-02 13:51:09
I'm doing an exercise in C but I have a problem when at the and I want to repeat the cicle (do while), infact if I type 1 the programme starts again by the top, but it doesn't stop at the gets(testo); . I tried plenty of ways to solve the bug without a solution, can anyone help me? #include <stdlib.h> #include <stdio.h> #include <string.h> main(){ int cch, cw, i, j, w, ord, f; //counter and index char testo[80]; char alfa[50][25]; char swap[25]; do{ cch=0; cw=0; j=0; w=0; f=0; for(i=0;i<80;i++){ testo[i]='\0'; } printf("Write the text:\n"); gets(testo); //initialization 2d array for(i=0;i<50;i

C: gets() skips the first input [closed]

半城伤御伤魂 提交于 2019-12-02 08:12:08
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> typedef struct record { char name[20]; char surname[20]; char telephone[20]; }Record; typedef struct node { Record data; struct node *next; }Node; Node *head = NULL; void addRecord(Record s) { Node *newNode; Node *n; newNode = (Node*)malloc(sizeof(Node)); newNode->data = s; newNode->next = NULL; if (head == NULL) { // The linked list is empty head = newNode; } else { // Traverse the whole list until we arrive at the last node n = head; while (n->next != NULL) { n = n->next; } n->next = newNode; } } Record

Is character array in C dynamic?

爱⌒轻易说出口 提交于 2019-12-02 07:09:28
I have written a simple program in C. A program to input a String and display it along with the length. #include<stdio.h> int main() { char a[4]; printf("Enter the name : "); gets(a); printf("\nThe name enterd is : %s",a); printf("\nLength of string is : %d",strlen(a)); getch(); return 0; } The program do not contain warning or error. At run-time I entered the value " melwinsunny " as input. There was no error and the result displayed was : Enter the name : melwinsunny The name entered is : melwinsunny length of string is : 11 Why is it so? I have declared the character array of length 4 (

Difference between fgets and gets

可紊 提交于 2019-12-02 04:56:18
问题 What is the difference between fgets() and gets() ? I am trying break my loop when the user hits just "enter". It's working well with gets() , but I don't want to use gets() . I tried with fgets() and scanf() but I don't have the same results as with gets() . fgets() breaks the loop whatever user enters in text! Here is my code : 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:

Dev-C++ Input skipped

限于喜欢 提交于 2019-12-02 03:10:26
问题 #include<stdio.h> #include<conio.h> main() { int i; char c, text[30]; float f; printf("\nEnter Integer : "); scanf("%d",&i); printf("\nEnter Character : "); c = getch(); printf("\nEnter String:"); gets(text); printf("\nEnter Float:"); scanf("%f",&f); printf("\nInteger : %d",i); printf("\nCharacter : %c8",c); printf("\nString : %s",text); printf("\nFloat : %f",f); getch(); } Why is this simple program not able to read a string using the gets() function? What else should I use to correct it?

Dev-C++ Input skipped

醉酒当歌 提交于 2019-12-02 00:15:54
#include<stdio.h> #include<conio.h> main() { int i; char c, text[30]; float f; printf("\nEnter Integer : "); scanf("%d",&i); printf("\nEnter Character : "); c = getch(); printf("\nEnter String:"); gets(text); printf("\nEnter Float:"); scanf("%f",&f); printf("\nInteger : %d",i); printf("\nCharacter : %c8",c); printf("\nString : %s",text); printf("\nFloat : %f",f); getch(); } Why is this simple program not able to read a string using the gets() function? What else should I use to correct it? Well it it worked in Turbo C in my old 32-bit PC but not here... Scanf or other input parsing functions

Take user input with JavaScript in the console

徘徊边缘 提交于 2019-12-01 21:21:36
问题 I need to get user input when running a .js in a console with spidermonkey like this: $ js myprogram.js What's the JavaScript equivalent of Ruby's gets ? 回答1: As far as I know, there is a readline() function, but it is a specific function for spidermonkey, it isn't a part of javascript. Example: 1)readline-test.js: print("Type some text and press <ENTER>:\t"); var userInput = readline(); print("User input: " + userInput); 2)js readline-test.js For more information see https://developer