Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- static char **read_lines(FILE *in, int *n) {
- char **lines = NULL;
- char line[BUFSIZ];
- *n = 0;
- while (fgets(line, BUFSIZ, in)) {
- lines = (char **) realloc(lines, ++(*n) * sizeof(char *));
- lines[*n - 1] = (char *) malloc(strlen(line) + 1);
- strcpy(lines[*n - 1], line);
- }
- return lines;
- }
- static void free_lines(char **lines, int n) {
- int i;
- for (i = 0; i < n; i++)
- free(lines[i]);
- free(lines);
- }
- int main(const int argc, const char * const *argv) {
- FILE *in;
- char **lines;
- int i, n;
- if (argc != 1 + 1) {
- fprintf(stderr, "usage: %s DATA_FILE\n", argv[0]);
- exit(1);
- }
- in = fopen(argv[1], "r");
- lines = read_lines(in, &n);
- for (i = 0; i < n; i += 2)
- printf("%s", lines[i]);
- free_lines(lines, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement