fgets

fgets maximum size read

拥有回忆 提交于 2019-12-06 08:03:48
Using fgets to input a string, I have doubts related to length of string read. For example, consider the following program. char str[50]; int i; int len; printf("Enter name:\n"); fgets(str,11,stdin); len = strlen(str); printf("len : %d\n",len); If I enter 123456789 , strlen gives 10. If I enter 1234567890 , strlen given is 10 again ?? I think strlen is considering newline also for string length. Am I correct? (I understand fgets using newline as part of string) What's wrong with (2) where I enter exactly 10 characters, Here string length should be 11 right? 10 + 1 (for newline) = 11 fgets

How to remove extra characters input from fgets in C?

让人想犯罪 __ 提交于 2019-12-06 05:29:32
I heard using gets() is bad in C programming and it's safer using fgets... So I am using fgets. However, I encounter a problem with fgets: I entered too much characters and somehow, it overflows. How to get rid of the extra input characters? char answer[4]; char answer2[4]; fgets(answer,sizeof(answer),stdin); printf("answer: %s\n",answer); fgets(answer2,sizeof(answer2),stdin); printf("answer2: %s\n",answer2); For example, for the first fgets, I enter 123456, the output I get is answer: 123 answer2: 456 How do I remove the 456 from going into the next fgets input? I want the output like this

Php Save fopen Result To A String

故事扮演 提交于 2019-12-06 05:20:47
How can I get the "$buffer" value into a string, and use it outside the fopen and fclose functions? Thanks. $handle = @fopen("http://www.example.com/", "r"); if ($handle) { while (!feof($handle)) { $buffer = fgetss($handle, 5000); echo $buffer ; } fclose($handle); } $handle = @fopen("http://www.example.com/", "r"); $buffer = ''; if ($handle) { while (!feof($handle)) { $buffer .= fgetss($handle, 5000); } fclose($handle); } Ale Try file_get_contents() : $buffer = file_get_contents("/my/file.txt"); The easiest way is to use file_get_contents: $buffer = file_get_contents("http://www.exemple.com");

Clear input buffer after fgets() in C

*爱你&永不变心* 提交于 2019-12-06 02:09:34
问题 #include <stdio.h> int main() { char name[10]; for(int i=0;i<=10;i++) { printf("Who are you? "); if(fgets(name,10,stdin)!=NULL) printf("Glad to meet you, %s.\n",name); } return(0); } When I input something greater than 10 characters the loop skips. Who are you? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Glad to meet you, aaaaaaaaa. Who are you? Glad to meet you, aaaaaaaaa. Who are you? Glad to meet you, aaaaaaaaa. Who are you? Glad to meet you, aaaaaaaaa. Who are you? Glad

Detecting EOF with fgets() where filesteam is stdin

不想你离开。 提交于 2019-12-06 01:34:08
Bit of background, I'm writing a program that plays the game "boxes" it runs in linux command line and is written in C. There's a prompt that waits for user input and then is read with fgets() and interpretted etc. As part of the task specification I have to return a specific error if I reach "End of file while waiting for user input". I understand that fgets() returns null when it reaches EOF... but say I have fgets(input,max_buffer,stdin); in a prompt loop if the user exits prematurely say with CTRL+C or CTRL+D does this mean that input == NULL? Can I even detect when a user does this with

Code not reaching statements using fgets?

帅比萌擦擦* 提交于 2019-12-05 21:22:39
I have written code that uses the fgets function with multiple conditions that call other functions within the code, namely aMethod and bMethod. int main(void) { char buffer[1024]; while (fgets(buffer, 1024, stdin)) { if ((strcasecmp(buffer,"a")) == 0) { aMethod(); } if ((strcasecmp(buffer, "b")) == 0) { bMethod(); } } } I'm not sure why it doesn't reach the if statements. Any help would be great, thankyou. If in doubt, print it out: int main(void) { char buffer[1024]; while (fgets(buffer, 1024, stdin)) { fprintf(stderr, "BUFFER is [%s]\n", buffer); /* <==== */ if ((strcasecmp(buffer,"a")) ==

Reading data from fsockopen using fgets/fread hangs

一曲冷凌霜 提交于 2019-12-05 20:54:22
问题 Here is the code that I am using: if (!($fp = fsockopen('ssl://imap.gmail.com', '993', $errno, $errstr, 15))) echo "Could not connect to host"; $server_response = fread($fp, 256); echo $server_response; fwrite($fp, "C01 CAPABILITY"."\r\n"); while (!feof($fp)) { echo fgets($fp, 256); } I get the first response: OK Gimap ready for requests from xx.xx.xx.xx v3if9968808ibd.15 but then the page times out. I have searched through stream_set_blocking, stream_set_timeout, stream_select, fread, etc.

PHP fscanf vs fgets

那年仲夏 提交于 2019-12-05 19:36:48
I am able to read an entire string in a line using fgets() but fscanf() is not doing so. According to PHP manual fscanf — Parses input from a file according to a format The function fscanf() is similar to sscanf() , but it takes its input from a file associated with handle and interprets the input according to the specified format, which is described in the documentation for sprintf() . fgets — Gets line from file pointer Gets a line from file pointer. Since fscanf() supports formats as mentioned above I can use %s to read an entire line but instead it reads only one word. The PHP

Why aren't scanf(“%*[^\\n]\\n”); and scanf(“%*[^\\n]%*c”); clearing a hanging newline?

房东的猫 提交于 2019-12-05 16:19:49
After a call to scanf("%d", &variable); we are left with a newline hanging at the stdin , which should be cleared before a call to fgets , or we end up feeding it a newline and making it return prematurely. I've found answers suggesting using scanf("%*[^\n]%*c"); after the first call to scanf to discard the newline and others suggesting using scanf("%*[^\n]\n"); . Theoretically, both should work: The first would consume everything that isn't a newline (but not including the newline itself) and then consume exactly one character (the newline). The second would consume everything that isn't a

file_get_contents with empty file not working PHP

▼魔方 西西 提交于 2019-12-05 09:17:48
I try to use file_get_contents form PHP but it's not working. There is my code : $filename = "/opt/gemel/test.txt"; if($filecontent = file_get_contents($filename)){ $nom = fgets(STDIN); file_put_contents($filename, $nom, FILE_APPEND); } else echo "fail"; And my file test.txt is empty. (0 octets). He exists but he is empty. When i write something into it, my code works perfectly but if he is empty my code echo "fails" Why that, why he can't open the file text.txt ? The function file_get_contents returns the string that's in the file. If the file contains no data, then file_get_contents returns