Thread: Spilting a large text file by searching a key word.

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    2

    Spilting a large text file by searching a key word.

    I have working code that will split the text file in half also code that I can split at a certain amount of lines. However, this is not what I need. Is there a way to search for a keyword every time the program sees this word it writes a file? I want to split the file every time the text file says CHAPTER.
    Here is the code I have for splitting the code in half.



    Code:
        #include <stdio.h>
          #include <string.h>
          #define MAX 256
        
          int main() {
                int i = 0, line = 0;
                FILE *fp1, *fp2, *fp3;
                char file1[MAX], file2[MAX], file3[MAX], str[MAX];
        
                /* get the name without file format(.txt) from user */
                printf("Enter your input file name:");
                scanf("%s", file1);
        
                /* form the output file names */
                sprintf(file2, "%s_part_%d.txt", file1, 1);
                sprintf(file3, "%s_part_%d.txt", file1, 2);
                strcat(file1, ".txt");
        
                /* open the input file in read mode */
                fp1 = fopen(file1, "r");
        
                /* error handling */
                if (!fp1) {
                        printf("Unable to open input file in read mode!!\n");
                        return 0;
                }
        
                /* open the output file in write mode */
                fp2 = fopen(file2, "w");
        
                /* error handling */
                if (!fp2) {
                        printf("Unable to open output file in write mode\n");
                        fclose(fp1);
                        return 0;
                }
        
                /* open the output file in write mode */
                fp3 = fopen(file3, "w");
        
                /* error handling */
                if (!fp3) {
                        printf("Unable to open output file2 in write mode\n");
                        fclose(fp1);
                        fclose(fp2);
                        return 0;
                }
        
                /* calculate the number of lines in input file */
                while (!feof(fp1)) {
                        fgets(str, MAX, fp1);
                        if (!feof(fp1))
                                line++;
                }
        
                /* moving the file pointer to the start of file */
                rewind(fp1);
        
                /* writing first half of data to first output file */
                while (i < line/2) {
                        fgets(str, MAX, fp1);
                        if (!feof(fp1)) {
                                fputs(str, fp2);
                        }
                        i++;
                }
        
                /* writing second half to second output file */
                while (!feof(fp1)) {
                        fgets(str, MAX, fp1);
                        if (!feof(fp1)) {
                                fputs(str, fp3);
                        }
                }
        
                /* close all opened files */
                fclose(fp1);
                fclose(fp2);
                fclose(fp3);
        
                /* remove the input file */
                remove(file1);
                return 0;
          }

  2. #2
    Registered User
    Join Date
    Jul 2016
    Posts
    2
    I just found out about the sscanf(). I will edit this post once I play with it more.

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    Break the problem down.

    1. You need to find the word Chapter.
    2. You need to find the file ending position for Chapter (either another chapter or eof).
    3. You need to copy the contents between these two markers.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
            /* calculate the number of lines in input file */
            while (!feof(fp1)) {
                    fgets(str, MAX, fp1);
                    if (!feof(fp1))
                            line++;
            }
    This can be simplified (and actually made safer) by doing
    Code:
    while ( fgets(str, MAX, fp1) != NULL ) {
      line++;
    }
    // when the loop exits, one of feof() or ferror() is true
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching a specific string/word/etc in a text file?
    By zacharyrs in forum C Programming
    Replies: 5
    Last Post: 11-29-2009, 07:54 PM
  2. reading text-and-numbers file word by word
    By bored_guy in forum C Programming
    Replies: 22
    Last Post: 10-26-2009, 10:59 PM
  3. searching a text file for a word
    By satory in forum C Programming
    Replies: 5
    Last Post: 02-22-2005, 01:04 PM
  4. Searching a VERY large text file
    By Tankndozer in forum C Programming
    Replies: 4
    Last Post: 07-29-2004, 02:45 AM
  5. Replies: 2
    Last Post: 01-02-2002, 02:05 PM

Tags for this Thread