fgets

strstr not functioning

本小妞迷上赌 提交于 2019-11-26 12:47:14
问题 Why does this particular piece of code return false on the strstr() if I input \"test\"? char input[100]; int main() { fgets(input, 100, stdin); printf(\"%s\", input); if(strstr(\"test message\", input)) { printf(\"strstr true\"); } } I thought strstr searched the first param for instances of the second param? It works when I replace input with some text or just assign it something directly, but it seems to not work with fgets. 回答1: It's because fgets stores the newline character so when

PHP - Returning the last line in a file?

主宰稳场 提交于 2019-11-26 11:26:33
问题 I\'m guessing it\'s fgets, but I can\'t find the specific syntax. I\'m trying to read out (in a string I\'m thinking is easier) the last line added to a log file. 回答1: The simplest naive solution is simply: $file = "/path/to/file"; $data = file($file); $line = $data[count($data)-1]; Though, this WILL load the whole file into memory. Possibly a problem (or not). A better solution is this: $file = escapeshellarg($file); // for the security concious (should be everyone!) $line = `tail -n 1 $file

fgets() includes the newline at the end [duplicate]

帅比萌擦擦* 提交于 2019-11-26 09:14:18
问题 This question already has an answer here: Removing trailing newline character from fgets() input 12 answers fgets(input,sizeof(input),stdin); if (strcmp(input, \"quit\") == 0){ exit(-1); } If I type quit, it does not exit the program; I\'m wondering why this is the case. By the way input is declared as char *input; . 回答1: Trailing newline in your input. See man fgets. Test for "quit" + newline, for example: fgets(input,sizeof(input),stdin); if(strcmp(input, "quit\n") == 0){ exit(-1); } I

Is fgets() returning NULL with a short buffer compliant?

南楼画角 提交于 2019-11-26 09:05:21
问题 In unit testing a function containing fgets() , came across an unexpected result when the buffer size n < 2 . Obviously such a buffer size is foolish, but the test is exploring corner cases. Simplified code: #include <error.h> #include <stdio.h> void test_fgets(char * restrict s, int n) { FILE *stream = stdin; s[0] = 42; printf(\"< s:%p n:%d stream:%p\\n\", s, n, stream); char *retval = fgets(s, n, stream); printf(\"> errno:%d feof:%d ferror:%d retval:%p s[0]:%d\\n\\n\", errno, feof(stream),

fgets instructions gets skipped.Why?

為{幸葍}努か 提交于 2019-11-26 05:39:44
问题 Whenever I do a scanf before a fgets the fgets instruction gets skipped. I have come accross this issue in C++ and I remember I had to had some instrcution that would clear the stdin buffer or something like that. I suppose there\'s an equivalent for C. What is it? Thanks. 回答1: I'll bet it's because of the \n stuck in the input stream. See one of these questions: I am not able to flush stdin. How do I go about Flushing STDIN here? scanf() causing infinite loop or this answer. Also: Why not to

Why is the gets function so dangerous that it should not be used?

房东的猫 提交于 2019-11-26 03:45:49
问题 When I try to compile C code that uses the gets() function with GCC, I get this warning: (.text+0x34): warning: the `gets\' function is dangerous and should not be used. I remember this has something to do with stack protection and security, but I\'m not sure exactly why. How can I remove this warning and why is there such a warning about using gets() ? If gets() is so dangerous then why can\'t we remove it? 回答1: In order to use gets safely, you have to know exactly how many characters you

C - scanf() vs gets() vs fgets()

有些话、适合烂在心里 提交于 2019-11-26 03:16:10
问题 I\'ve been doing a fairly easy program of converting a string of Characters (assuming numbers are entered) to an Integer. After I was done, I noticed some very peculiar \"bugs\" that I can\'t answer, mostly because of my limited knowledge of how the scanf() , gets() and fgets() functions work. (I did read a lot of literature though.) So without writing too much text, here\'s the code of the program: #include <stdio.h> #define MAX 100 int CharToInt(const char *); int main() { char str[MAX];

Read each line of txt file to new array element

心已入冬 提交于 2019-11-26 02:28:36
问题 I am trying to read every line of a text file into an array and have each line in a new element. My code so far. <?php $file = fopen(\"members.txt\", \"r\"); $i = 0; while (!feof($file)) { $line_of_text = fgets($file); $members = explode(\'\\n\', $line_of_text); fclose($file); ?> 回答1: If you don't need any special processing, this should do what you're looking for $lines = file($filename, FILE_IGNORE_NEW_LINES); 回答2: The fastest way that I've found is: // Open the file $fp = @fopen($filename,

strcmp on a line read with fgets

不羁岁月 提交于 2019-11-26 00:35:00
问题 I\'m trying to compare two strings. One stored in a file, the other retrieved from the user (stdin). Here is a sample program: int main() { char targetName[50]; fgets(targetName,50,stdin); char aName[] = \"bob\"; printf(\"%d\",strcmp(aName,targetName)); return 0; } In this program, strcmp returns a value of -1 when the input is \"bob\" . Why is this? I thought they should be equal. How can I get it so that they are? 回答1: strcmp is one of the few functions that has the reverse results of true

Why is the gets function so dangerous that it should not be used?

旧巷老猫 提交于 2019-11-25 23:55:17
问题 When I try to compile C code that uses the gets() function with GCC, I get this warning: (.text+0x34): warning: the `gets\' function is dangerous and should not be used. I remember this has something to do with stack protection and security, but I\'m not sure exactly why. How can I remove this warning and why is there such a warning about using gets() ? If gets() is so dangerous then why can\'t we remove it? 回答1: In order to use gets safely, you have to know exactly how many characters you