fgets

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:

Write an EOF to a pipe

孤街浪徒 提交于 2019-12-02 04:51:43
I have a parent and a child prozess and want to write an EOF from the parent to the child via a pipe... how does this work? here is my attampt: ---parent code--- if(dup2(parent_pipe[1], STDOUT_FILENO) == -1) { /*stdout gets closed and parent_pipe is duplicated with id of stdout*/ error("Can't duplicate the write-end of the pipe to stdout!"); } if(close(parent_pipe[0]) == -1) { error("Can't close read-end of the pipe!"); } char blub[] = "EOF"; if(write(parent_pipe[1], blub, strlen(blub)) == -1) { error("Failed to write the array"); } ---child code--- if(dup2(parent_pipe[0], STDIN_FILENO) == -1)

fgets is getting skipped

别等时光非礼了梦想. 提交于 2019-12-02 04:41:17
I have little program where I want to ask for an option and then for a filename. //Some code before printf("######################\n"); printf("# 1. Register a file #\n"); printf("# 2. Get global list #\n"); printf("# 3. Download a file #\n"); printf("# 4. Quit / Exit #\n"); printf("######################\n"); printf("Enter decision: "); fflush(stdin); action = getchar(); action -= '0'; sprintf(input, "[%d]--", action); switch (action) { case 1: printf("Enter file name: "); fflush(stdin); fgets(input+strlen(input), LINE_LEN, stdin); input[strlen(input)] = (input[strlen(input)] == '\n') ? 0 :

Does fgets() always null-terminate the string it returns?

感情迁移 提交于 2019-12-02 04:39:19
Is this safe to do? Does fgets terminate the buffer with null or should I be setting the 20th byte to null after the call to fgets and before I call clean ? // strip new lines void clean(char *data) { while (*data) { if (*data == '\n' || *data == '\r') *data = '\0'; data++; } } // for this, assume that the file contains 1 line no longer than 19 bytes // buffer is freed elsewhere char *load_latest_info(char *file) { FILE *f; char *buffer = (char*) malloc(20); if (f = fopen(file, "r")) if (fgets(buffer, 20, f)) { clean(buffer); return buffer; } free(buffer); return NULL; } Yes fgets() always

Program is skipping fgets without allowing input

久未见 提交于 2019-12-02 04:12:30
问题 Basically as the title says.. When my program is run from the console, it'll ask if you'd like to encrypt or decrypt.. and when I input e or E, it creates a new blank line (until I input some kind of text), then shows the "enter the text" and "enter the key" lines all at once.. So, in the console it would look something like: Would you like to (E)ncrypt or (D)ecrypt? e asdf jkl; <---- random user input to get the program to continue.. Enter the text you would like to encrypt : Enter a key to

How fget works?

冷暖自知 提交于 2019-12-02 03:44:19
I am using gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2 I am writing a very simple script to take string as input and print the same with the some custom message. First user enters the T(no. of times to take string) and then takes input by fgets . . I used this & this as reference. I am getting a very strange output ie fgets is adding some extra new lines and even loop is not working properly for T=2 it is asking input only once. Can anybody explain me what is wrong with snippet. Thanks in advance! #include<stdio.h> #include<string.h> int main() { int T; scanf("%d",&T); while(T--){ char str[100]={""};

Program is skipping fgets without allowing input

一个人想着一个人 提交于 2019-12-02 01:01:59
Basically as the title says.. When my program is run from the console, it'll ask if you'd like to encrypt or decrypt.. and when I input e or E, it creates a new blank line (until I input some kind of text), then shows the "enter the text" and "enter the key" lines all at once.. So, in the console it would look something like: Would you like to (E)ncrypt or (D)ecrypt? e asdf jkl; <---- random user input to get the program to continue.. Enter the text you would like to encrypt : Enter a key to use for encryption : (user input) and then the program exits.. //message to be encrypted char text[250]

POJ - 3094 - Quicksum = 水题

淺唱寂寞╮ 提交于 2019-12-02 00:38:23
http://poj.org/problem?id=3094 学习fgets的使用,注意fgets是会连换行一起保存的。 #include<algorithm> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<map> #include<set> #include<stack> #include<string> #include<queue> #include<vector> using namespace std; typedef long long ll; char s[10000]; int main() { #ifdef Yinku freopen("Yinku.in", "r", stdin); #endif // Yinku while(1) { fgets(s + 1, 10000 - 1, stdin); if(s[1] == '#') break; ll sum = 0; int n = strlen(s + 1); for(int i = 1; i <= n; ++i) { if(isupper(s[i])) sum += 1ll * i * (s[i] - 'A' + 1); } printf("%lld\n", sum); } } 来源: https

If statement being ignored in main function [duplicate]

社会主义新天地 提交于 2019-12-02 00:36:07
问题 This question already has answers here : strcmp on a line read with fgets (6 answers) Closed 4 years ago . I'm currently writing a code in C and my if statement in main function is being ignored. As you can see, this code receives some string as input and applies the Caesar's Cipher. Note: Function ciphering called in main is also defined, I just don't paste because I don't think it is necessary because the problem is that when I ask the user if he wants to encrypt or decrypt the text, after

Issue in C language using 'fgets' after 'printf' as 'fgets' runs before 'printf' [duplicate]

淺唱寂寞╮ 提交于 2019-12-01 20:56:34
Possible Duplicate: Why does printf not flush after the call unless a newline is in the format string? (in C) I am getting a problem using printf and fgets as in my code printf is written earlier then fget but it does not run, it runs after fgets runs. enum { max_string = 127 }; static char string[max_string+1] = ""; int main( int argc, char ** argv ) { printf("Type a String: "); fgets(string, max_string, stdin); printf("The String is %s\n", string); return 0; } do a flush of the stdout fflush(stdout); before fgets(...) printf("Type a String: "); fflush(stdout); fgets(string, max_string, stdin