Find strings within * .txt files in C

-2

I'm a beginner and I'm trying to make a program and C ++ that requires two functions.

1) Search in a * .txt file for a specific string;

2) By some kind of index indicate the position of this string within the file.

To try to explain better, for example, I have the text "Name: Luiz Fernando Oliveira". What I need is a program that finds the string "Name:" and tells me the position of the file so that I can read "Luiz Fernando de Oliveira" and save it to another file.

I know you have a lot of stuff on it but I can not get the pieces to fit together. Any help is welcome.

Thank you in advance.

    
asked by anonymous 20.12.2016 / 02:37

1 answer

2

What you need is:

  • Open the input file.

  • Determine the size of the file.

  • Put the entire contents of the file into a string in memory.

  • Search for Nome: in this string, finding the appropriate position.

  • Separate the name into another string.

  • Open the output file.

  • Write to the output file.

  • Close both files.

  • To open the file, use the fopen function. In step 1, you should open in binary read mode (% with%). In step 6, use binary write mode ( "rb" or "wb" ). Check out my other answer for more details.

    To do step 2, according to my old answer , use this:

    fseek(fp, 0L, SEEK_END);
    int sz = ftell(fp);
    fseek(fp, 0L, SEEK_SET);
    

    In step 3, you use a "ab" to allocate enough memory for the string and use malloc to read the contents of the file.

    One possible way to do step 4 would be:

    • Make a function that looks for a string within another string. fread . The analogy used is to get a needle in the haystack, where the haystack is the contents of the file and the needle is what you are looking for there.

    • In this function, you can use two int busca_string(char *agulha, char *palheiro, int tamanho_agulha, int tamanho_palheiro) loops inside one another. The outer loop traverses each character of the read file string (the for ). The inner loop compares from the position of the outer loop, if the characters found in palheiro correspond to the same sequence of characters you are looking for ( palheiro , which is Nome: ). >

    • Use agulha in the inner loop when what you find in break does not match what you are examining in palheiro .

    • After the end of the inner loop, but still inside the outer loop, check that the inner loop is over, and if it is finished, give agulha .

    • If the outer loop terminates, give return 1; .

    • Be careful not to access memory beyond the limit of any of the two strings.

    Step 5, I do not know. You did not say how you will know where the name you are looking for ends so that you can separate it from the subsequent content. However the initial position of this content is the position resulting from step 4 plus the size of return 0; .

    For step 7, use agulha .

    For step 8, use fwrite . Do not forget to call fclose for every free .

    If you prefer to use text mode instead of binary ( malloc , "r" and "w" instead of "a" , "rb" and "wb" ), change "ab" to fwrite and fprintf by fread . But in this case, the size of the allocated memory area may end up being insufficient if line-break conversions of type fgets or \r -> \r\n occur. Therefore, I recommend using binary mode in reading. In writing, use whatever mode you think is best (but to avoid surprises, it might be best to stick to the binary).

        
    20.12.2016 / 03:30