Advertisement
cd62131

EvenLines

Jul 29th, 2014
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. static char **read_lines(FILE *in, int *n) {
  5.   char **lines = NULL;
  6.   char line[BUFSIZ];
  7.   *n = 0;
  8.   while (fgets(line, BUFSIZ, in)) {
  9.     lines = (char **) realloc(lines, ++(*n) * sizeof(char *));
  10.     lines[*n - 1] = (char *) malloc(strlen(line) + 1);
  11.     strcpy(lines[*n - 1], line);
  12.   }
  13.   return lines;
  14. }
  15. static void free_lines(char **lines, int n) {
  16.   int i;
  17.   for (i = 0; i < n; i++)
  18.     free(lines[i]);
  19.   free(lines);
  20. }
  21. int main(const int argc, const char * const *argv) {
  22.   FILE *in;
  23.   char **lines;
  24.   int i, n;
  25.   if (argc != 1 + 1) {
  26.     fprintf(stderr, "usage: %s DATA_FILE\n", argv[0]);
  27.     exit(1);
  28.   }
  29.   in = fopen(argv[1], "r");
  30.   lines = read_lines(in, &n);
  31.   for (i = 0; i < n; i += 2)
  32.     printf("%s", lines[i]);
  33.   free_lines(lines, n);
  34.   return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement