fgets

how to prevent fgets blocks when file stream has no new data.

I have a popen() function which executes "tail -f sometextfile". Aslong as there is data in the filestream obviously i can get the data through fgets(). Now, if no new data comes from tail, fgets() hangs. I tried ferror() and feof() to no avail. How can i make sure fgets() doesn't try to read data when nothing new is in the file stream? ...

What is it about PHP's fgets() that makes it so horrible on large files?

What is it about fgets() implementation that makes it so horrible on large files vs fread? To demonstrate, run this code: <?php $line = str_repeat('HelloWorld', 100000) . "\n"; for($i=0; $i<10000; ++$i) file_put_contents('myfile', $line, FILE_APPEND); //now we have roughly a 1gig file // We'll even let fread go first incase // the...

Why does fgets hang on a certain url on wamp, but is fine on mamp?

I have a script that reads RSS feeds using fopen & fgets. When trying to the feed at: http://rss.fok.nl/feeds/nieuws my script hangs until the max_timeout of the PHP is reached. The thing is: it worked perfectly (on the same url) until today. it still works on my development mac MAMP server. it doesn't work on the production WAMP ser...

Most efficient way to retrieve the source of a website through PHP? (GET Request)

I know that file_get_contents can be used to retrieve the source of a webpage, but I want to know the most efficient way. I have an old class I made a long time ago that uses something like this: $this->socket = fsockopen($this->host, 80); fputs($this->socket, 'GET ' . $this->target . ' HTTP/1.0' . "\n"); fputs($this->socket, '...

Read a line of input faster than fgets?

I'm writing a program where performance is quite important, but not critical. Currently I am reading in text from a FILE* line by line and I use fgets to obtain each line. After using some performance tools, I've found that 20% to 30% of the time my application is running, it is inside fgets. Are there faster ways to get a line of text?...

Communication problem between Java and C++ app on stdin

I have a java app here that starts a C++ app via the java.lang.Process API and then tries to send commands to it via the stdin pipe: process.getOutputStream().write("foo\n"); process.getOutputStream().flush(); On the C++ side there's a loop running that checks for input in stdin and if there is some it reads it. Unfortunately the che...

Looping Fget with fsockopen in PHP 5.x

Hello, I have a Python Server finally working and responding to multiple command's with the output's, however I'm now having problem's with PHP receiving the full output. I have tried commands such as fgets, fread, the only command that seems to work is "fgets". However this only recieve's on line of data, I then created a while statem...

PHP - Returning the last line in a file?

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. ...

convert a char* to std::string

I need to use std::string to store data retrieved by fgets(). To do this I need to convert fgets() char* output into an std::string to store in an array. How can this be done? ...

Difference between scanf() and fgets().

I want to know what is the difference between fgets() and scanf(). I am using C as my platform. ...

PHP fgets() (or fread()) does not block.

Hello everybody, I'm trying to connect to a beanstalkd server through a PHP script and to reserve jobs from an existing queue. I'm using the fgets() function to get responses from the deamon, expecting the script to hang unless a job is available, here's a sample code: set_time_limit(0); $connection = fsockopen('localhost', 11300); fwr...

Calling fgets() on popen() of 'ssh' is flushing the beginning of stdin of the calling process (ptty issue)

Hello, I have now whittled this down to a minimal test case. Thus far I have been able to determine that this is an issue related to pseudo-terminals which come about with the pipe of ssh. Adding the '-t -t' to the ssh call improved things, in that now, it takes a second call to fgets() to cause the issue. I suspect that the stderr o...

Mimic Python's strip() function in C

Hi all, I started on a little toy project in C lately and have been scratching my head over the best way to mimic the strip() functionality that is part of the python string objects. Reading around for fscanf or sscanf says that the string is processed upto the first whitespace that is encountered. fgets doesn't help either as I still...

Fgets in C++ repeats last line

I have program like (from link text) FILE* soubor; char buffer[100]; soubor = fopen("file","r"); string outp = ""; while (! feof(soubor)) { fgets(buffer,100,soubor); fputs (buffer , stdout); } fclose(soubor); and file like A B C D E and the output of program is A B C D E E it repeats last line of file twice. I ha...

Why is the `gets' function is dangerous? Why should not be used?

When I try to compile C code that uses the gets function, I get a warning: warning: the gets function is dangerous and should not be used. I remember this has to do something with stack protection and security, but I'm not sure. Can someone help me with removing this warning and explain why is there such warning? If gets is "dangerous" ...

How to retrieve data and not entire lines in C?

Hi, Right now I use: char record[BUFLEN]; if(fgets(record, BUFLEN, fp) != NULL) { /* some code */ } to get lines from input like: city=Boston;name=Bob;age=35 city=New York;name=Michael;age=29 Can I use something else in C that would give me not entire lines until '\n' but separate pairs like: "city=Boston" t...

My http server in c++ is not sending all files back correctly

I'm working on an HTTP server in c++, and right now it works for requests of text files, but when trying to get a jpeg or something, only part of the file gets sent. The problem seems to be that when I use fgets(buffer, 2000, returned_file) it seems to increment the file position indicator much more than it actually ends up putting into ...

How to read a file line-by-line in C?

Okay, I'm a beginner when it comes to C, so bear with me. Here's the problem: I have a text file with up to 100 IP addresses, 1 per line. I need to read each address, as a string, into an array called "list". First, I'm assuming that "list" will need to be a two-dimensional char array. Each IP address is 11 characters in length, 12 if y...

How can a while() loop not stall when using fsockopen() and fgets() in PHP?

This is the basic connection code for a small PHP IRC bot. The problem is that it seems that the while() loop does not progress beyond fgets() until it receives data from the IRC server. I want the while() loop to iterate regardless if the IRC server has not yet sent data. Is this possible? $socket = fsockopen($config['irc_server'], $co...

insert text inside a line

I have a file pointer which I am using with fgets() to give me a complete line along with the new line in the buffer. I want to replace 1 char and add another character before the new line. Is that possible? For example: buffer is "12345;\n" output buffer is "12345xy\n" This is the code: buff = fgets((char *)newbuff, IO_BufferSize...