简体   繁体   中英

Search for a specific line in text file in C

I have a text file, and i want to get a specific line from this text file. The file content looks like,

<div class="logo_style">
   <img src="/images/Gnome-Network-Wireless-NoTx-64.png">
   <h2>Fast Actions</h2>
</div>
<div class="container">
   <p><b><font color=green>Action "update_frequency" is sent.</font></b></p>
   <br/><a href="/">[Back To Main Section]</a>
</div>
</body></html>

I want to search only the line Action "update_frequency" is sent and print it to stdout. I used the strstr function but when i print the pointer given by strstr , it gives all the ending lines too, which appear after the desired line in text file. So I tried to store the returned pointer in char *str_result . Then i copied the first 10 bytes of str_result in str_dst but I am getting the result as segmentation fault (core dumped) . Here is the following code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(int argc, char *argv[])
{

    FILE  *file_r;
    char *buffer;
    buffer = malloc (100000);
    char *str_result;
    char str_dst[100];
    file_r= fopen ("file_new.txt", "r");
    fseek (file_r, SEEK_SET, 0);
    while (fgets (buffer, 100000, file_r)!=NULL)
        {
            ;

        }

    str_result = strstr(buffer, "Action");
    memcpy (str_dst, str_result, 10);
    printf("%s\n", str_dst);


    fclose (file_r);
    return 0;
}

Can anyone please suggest that how I can get only the desired text line and not the other lines as shown by strstr?
Thanks in advance.

memcpy (str_dst, str_result, 10); does not insert/add the NULL terminator.

You must add str_dst[10]='\\0'; between memcpy and printf to avoid the segmentation fault

fgets (buffer, 100000, file_r);
str_result = strstr(buffer, "Action ");//add space
size_t len = strchr(str_result, '<') - str_result;//length upto '<'
memcpy (str_dst, str_result, len);
str_dst[len] = '\0';//null-terminate
printf("%s\n", str_dst);

Think of the text file as an array full of characters. Create several integer variables and let them hold 0. Create a for loop and if a specific letter appears, have one of the variables equal the place in the array the character was found. Next, think of another specific character and repeat the same above until you find it. Have one of the variables hold it's place. Repeat this cycle until you have tested the array for all of the characters in the string. Next, create a series of if statements testing the integer values, you can do this by taking the int place value of the second letter in the word, subtracting it from the int place value of the first, and if it equals 1, you know that letter 2 occurs directly after letter 1. This may seem confusing, and it's not an easy way to go about it but I hope this helps somewhat!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM