Skip to content

Instantly share code, notes, and snippets.

@SergKolo
Created November 29, 2019 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SergKolo/0c048ff4559815ee6caf58859d3b9dde to your computer and use it in GitHub Desktop.
Save SergKolo/0c048ff4559815ee6caf58859d3b9dde to your computer and use it in GitHub Desktop.
Basically simplified example from getline(2) man page with optional stdin vs file reading
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t nread;
stream = stdin;
if (argc == 2)
stream = fopen(argv[1],"r");
//while( (getline(&line,0,stream)) != -1 )
// printf("Line:%s\n",&line);
while( (getline(&line,&len,stream)) != -1 )
printf("%s",line);
free(line);
fclose(stream);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment