fgets

fgets() function in C

Hi, I know everybody has told me to use fgets and not gets because of buffer overflow. However, I am a bit confused about the third parameter in fgets(). As I get it, fgets is dependent on: char * fgets ( char * str, int num, FILE * stream ); char* str is the ptr to where my input will be stored. num is the max number of characte...

Can C's fgets be coaxed to work with a string *not* from a file?

Specifically, the code sample here works great, but only when the string is stored in a file. Sometimes I need it to process a generated string (stored in a string variable), but I'm having trouble convincing fgets's third parameter to work with string variables because it's a pointer to a FILE structure. Or perhaps there's a functio...

Reading and outputting UTF-8 strings in c/cocoa

In an objective-c/cocoa app, I am using c functions to open a text file, read it line-by-line and use some lines in a third-party function. In psuedo-code: char *line = fgets(aFile); library_function(line); // This function calls for a utf-8 encoded char * string This works fine until the input file contains special characters (such ...

capturing empty output returned from commandline and display some message if its empty

Thank you guys for answering "capturing commandline output directly in a buffer" .. thanks a lot.. I used popen() to get my results correct. here i have one more thing may be small one I need to print something in my window when the script "check.sh" returns nothing, means validation when no output from the script is returned. check.s...

Writing to a character array that is a member of a struct

Hello, this is my first time posting a question here - I've searched for ones that are similar, but none came up that I found. Here is the snippet from my header: #define LINE_LEN_MAX 256 typedef struct line_description { char buffer[LINE_LEN_MAX + 1]; [...] } line; And here is the snippet from my main function: int main(in...

fgets from stdin problems [C]

I'm writing a program that works with files. I need to be able to input data as structures, and eventually read it out. The problem i have at the moment is with this code: typedef struct { char* name; ..... }employeeRecord; employeeRecord record; char name[50]; if(choice == 1) { /*Name*/ printf("\nEnter the...

fgets - is ommiting the last line in c

Basically i am having problems with my code - this is homework so would rather not post it in here for obvious reasons. If it becomes truely important for me to do so then i will have to as i am so stuck. i am reading 2 text files and it also has a seperator , these values come from the command line, just assume that the seperator in th...

fgets and strcmp [C]

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

ignore extra spaces when using fgets

I'm using fgets with stdin to read in some data, with the max length I read in being 25. With one of the tests I'm running on this code, there are a few hundred spaces after the data that I want - which causes the program to fail. Can someone advise me as to how to ignore all of these extra spaces when using fgets and go to the next lin...

question about fgets

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

Are fopen/fread/fgets PID-safe in C ?

Various users are browsing through a website 100% programmed in C (CGI). Each webpage uses fopen/fgets/fread to read common data (like navigation bars) from files. Would each call to fopen/fgets/fread interefere with each other if various people are browsing the same page ? If so, how can this be solved in C ? (This is a Linux server, co...

PHP fastest method of reading server response

Hi there, im having some real problems with the lag produced by using fgets to grab the server's response to some batch database calls im making. Im sending through a batch of say, 10,000 calls and ive tracked the lag down to fgets causing the hold up in the speed of my application as the response for each call needs to be grabbed. I ...

fsockopen soap request

I am trying to send a SOAP message to a service using php. I want to do it with fsockopen, here's is the code : <?php $fp = @fsockopen("ssl://xmlpropp.worldspan.com", 443, $errno, $errstr); if (!is_resource($fp)) { die('fsockopen call failed with error number ' . $errno . '.' . $errstr); } $soap_out = "POST /xmlts HTTP/1.1\r\n...

Removing trailing newline character from fgets() input

i am trying to get some data from the user and send it to another function in gcc. the code is something like this. printf("Enter your Name: "); if(!(fgets(Name, sizeof Name, stdin) != NULL)) { fprintf(stderr, "Error reading Name.\n"); exit(1); } However, i find that it has an \n character in the end. so if i e...

Editing a remote file on-the-fly with PHP

Hi, I have a requirement to edit a remote text file on-the-fly, the content of which currently stands at ~1Mb. I have tried a couple of approaches and both seem to be clunky or hog memory which I can't rely on. Thinking out logically what I'm trying to achieve is: FTP to a remote server. Download a copy of the file for backup purpos...

How to open a file and remove the last line?

I am looking to open up a file, grab the last line in the file where the line = "?>", which is the closing tag for a php document. Than I am wanting to append data into it and add back in the "?>" to the very last line. I've been trying a few approaches, but I'm not having any luck. Here's what I got so far, as I am reading from a zip...

how to read a string from a \n delimited file

I'm trying to read a return delimited file. full of phrases. I'm trying to put each phrase into a string. The problem is that when I try to read the file with fscanf(file,"%50s\n",string); the string only contains one word. when it bumps with a space it stops reading the string ...

check whether fgets would block

Hi, I was just wondering whether in C is it possible to peek in the input buffer or perform similar trickery to know whether a call to fgets would block at a later time. Java allows to do something like that by calling BufferedReader.ready(), this way I can implement console input something like this: while (on && in.ready()) { line ...

fgets() and fread() - What is the difference?

I understand the differences between fgets() and fgetss() but I don't get the difference between fgets() and fread(), can someone please clarify this subject? Which one is faster? Thanks! ...

C child read giving "resource temporarily unavailable"

So I have a file stream from a parent process to a child - and most of the time it works fine. However, when reading from it multiple times quickly, using fgets() will return NULL and the error is set to "resource temporarily unavailable". The problem is intermittent - and running the script that does the reads will sometimes have fgets ...