Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

What is the most efficient way?

Name: Anonymous 2008-05-13 23:06

Ok, so I have socket in C, and I want to read one line from the socket, not including the ending new line. I want to write a function that returns a malloc'd string containing the line.

Name: Anonymous 2008-05-14 3:24

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *get_line_from_stream(FILE *stream){
 size_t len = 0;
 char *line = fgetln(stream, &len);
 char *res = calloc(line[len - 1] == '\n' ? len-- : len + 1, sizeof(char));
 strncpy(res, line, len);
 res[len] = '\0';
 return res;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List