Thread: Print only specific lines in the file

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    30

    Print only specific lines in the file

    I understand some perl and a bit of C++. I am just learning C and I been trying many examples that use strChr, strstr, rewind, scanf, Index, strtok, fseek and fsetpos for over a week. Now I am trying to figure which combination of these functions should I use, and how. What I want to do is open my file and copy only specific lines to another file. I hope my explanation is clear. This is a very large file with many lines to skip. They may be anywhere in the list before and after the keyword, PROLIST. I'm hoping for some examples.

    First, I need to print each line after every occurrences of the word PROLIST:

    (1.txt)
    Code:
    
     Skip this line.
      Skip this line.
      PROLIST
     Print this line
     Skip this line.
     PROLIST
     Print this line
     Skip this line.
     Skip this line.

    Also in a second example, could it show how to print each line before and after every occurrences of the word PROLIST:

    (A.txt)
    Code:
      Skip.
      Print this line
      PROLIST
      Print this line
      Skip.
      Skip.
      Print this line
      PROLIST
      Print this line
    
    I use Linux and BSD’s. I can post examples of what I have done so far with the listed C functions above; however, it seems that each instance worked perfectly for what it does but I hit a dead-end at what I’m trying to do with them. My first list wrapped is more important then the complicated one below it.

    Note to the
    Administrator:: Could you please correct the misalignment of the first block of text wrapped in the code tag. Nothing I tried fixed it.


    I appreciate any feedback.
    Last edited by fat32; 11-02-2018 at 01:48 AM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    627
    I might do it like this.
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <string.h>
     
    int main() {
        FILE *fin = fopen("1.txt", "r");
        FILE *fout = stdout; // for testing
     
        char line[1000];
        bool print_line = false;
     
        while (fgets(line, sizeof line, fin) != NULL) {
            if (print_line) {
                fputs(line, fout);
                print_line = false;
            }
            if (strstr(line, "PROLIST") != NULL)
                print_line = true;
        }
     
        fclose(fin);
        return 0;
    }
    Using strstr like I did means that it counts a line with "PROLIST" anywhere in it.
    We can be more specific if you want

    For the second one, here's a possibility.
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <string.h>
     
    int main() {
        FILE *fin = fopen("A.txt", "r");
        FILE *fout = stdout; // for testing
     
        char line[1000], last_line[1000] = {0};
        bool print_line = false;
     
        while (fgets(line, sizeof line, fin) != NULL) {
            if (print_line) {
                fputs(line, fout);
                print_line = false;
            }
            if (strstr(line, "PROLIST") != NULL) {
                fputs(last_line, fout);
                print_line = true;
            }
            strcpy(last_line, line);
        }
     
        fclose(fin);
        return 0;
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-19-2016, 06:51 AM
  2. Replies: 5
    Last Post: 02-06-2013, 09:11 AM
  3. Open file and print user selected number of lines
    By steals10304 in forum C Programming
    Replies: 14
    Last Post: 09-01-2009, 01:13 PM
  4. Circular linked list to print last N lines of file
    By FortinsM3 in forum C Programming
    Replies: 7
    Last Post: 10-24-2008, 05:42 PM
  5. Reading specific lines
    By Aristotle in forum C Programming
    Replies: 2
    Last Post: 04-12-2004, 04:34 PM

Tags for this Thread