Optimize string search in .txt file

Cata

This might be a really stupid question, but how do I optimize this code in making it more effiecient (faster,less memory consuming)? I made this code to help me sort some text files. It reads each string from the first file, then it searches through the second file until it finds all related strings, and in a third file it writes some matched strings. Here is the code:

ifstream h("SecondFile.txt");
ifstream h2("FirstFile.txt");
ifstream uh("MatchedStrings.txt");
ofstream g("sorted.txt");    
int main()
    {
        string x, y, z;
        cout << "Sorting..." << endl;;
        while (!h.eof()){
            h >> x;
            while (!h2.eof() || (y == x)){
                h2 >> y;
                uh >> z;
                if (y == x){
                    g << z << endl;
                    break;
                    h2.clear();
                    h2.seekg(0);
                    uh.clear();
                    uh.seekg(0);
                }
            }
            if (h2.eof() && (y != x)){
                g << "none" << endl;
                h2.clear();
                h2.seekg(0);
                uh.clear();
                uh.seekg(0);
            }
        }
        cout << "Finished!";
    }

I have changed my code to this:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
ifstream h("SecondFile.txt");
ifstream h2("FirstFile.txt");
ifstream uh("MatchedStrings.txt");
ofstream g("sorted.txt");

int main()
{
    string x;
    bool write_none = true;
    int i = 0,l=0;
    string check[] = {""};
    string unhashed_checked[] = { "" };
    string sorted_array[] = { "" };
    cout << "Sorting..." << endl;
    //Get to memory
    while (!h2.eof())
    {
        h2 >> check[i];
        uh >> unhashed_checked[i];
        i++;
    }

    while (!h.eof()){
        h >> x;
        write_none = true;
        for (int t = 0; t <= i;t++)
        {
            if (x == check[t])
            {
                break;
                write_none = false;
                sorted_array[l] = unhashed_checked[i];
                l++;
            }
        }
        if (write_none)
        {
            sorted_array[l] = "none";
            l++;
        }
    }
    for (int k = 0; k <= l; k++)
    {
        g << sorted_array[k]<<endl;
    }
    cout << "Finished!";
}

But I am getting this exception when running the program:

Unhandled exception at 0x01068FF6 in ConsoleApplication1.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC
Ami Tavory

Suppose the number of strings in your files are n and m, respectively.

The way you're doing it now, the complexity is Θ(n m). Moreover, the complexity constants are those of file operations, which are incredibly slow.

Instead, you should just read one of the files into a std::unordered_* container, then compare keys between the containers. This should reduce the running time to expected Θ(n + m).


As a side note, you might want to look at more modern ways to read strings into containers (using, e.g., std::istream_iterator).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

BufferedReader - Search for string inside .txt file

From Dev

Search txt file for string where middle substring is unkown

From Dev

batch file to search in txt file for string and get the full line off the string

From Dev

Optimize Speed of Recursive File Search in Subdirectories?

From Dev

How to search a txt file for regex?

From Dev

Search in a file for Specific sentences in a txt file in java

From Dev

Read all lines from TXT file and search only in every FIRST string of each line C#

From Dev

Read all lines from TXT file and search only in every FIRST string of each line C#

From Dev

search for string in json file

From Dev

Sum of string in cloned TXT file

From Dev

Fortran find string in txt file

From Dev

Remove string from txt file

From Dev

Finding string index in .txt file

From Dev

Search for file end with .txt in subdirectories in linux

From Dev

python, search .txt file and inject character

From Dev

Flag controlled while loop to search txt file

From Dev

Search and extract text out of a txt file

From Dev

How to create search tab and open the .txt file?

From Dev

search in txt file and put the result in text box

From Dev

python, search .txt file and inject character

From Dev

Search and replace from txt file on many files?

From Dev

Search number from 'txt A' and replace into csv file

From Dev

Search txt file for varying keyword - then assign to variable

From Dev

Search inside txt file using javascript

From Dev

Python3 search for input in txt file

From Dev

For a given input search the .txt file and if the search found print the whole line

From Dev

Python Parse log file string into txt file

From Dev

Search file names that contain a string

From Dev

search and replace part of string in file

Related Related

  1. 1

    BufferedReader - Search for string inside .txt file

  2. 2

    Search txt file for string where middle substring is unkown

  3. 3

    batch file to search in txt file for string and get the full line off the string

  4. 4

    Optimize Speed of Recursive File Search in Subdirectories?

  5. 5

    How to search a txt file for regex?

  6. 6

    Search in a file for Specific sentences in a txt file in java

  7. 7

    Read all lines from TXT file and search only in every FIRST string of each line C#

  8. 8

    Read all lines from TXT file and search only in every FIRST string of each line C#

  9. 9

    search for string in json file

  10. 10

    Sum of string in cloned TXT file

  11. 11

    Fortran find string in txt file

  12. 12

    Remove string from txt file

  13. 13

    Finding string index in .txt file

  14. 14

    Search for file end with .txt in subdirectories in linux

  15. 15

    python, search .txt file and inject character

  16. 16

    Flag controlled while loop to search txt file

  17. 17

    Search and extract text out of a txt file

  18. 18

    How to create search tab and open the .txt file?

  19. 19

    search in txt file and put the result in text box

  20. 20

    python, search .txt file and inject character

  21. 21

    Search and replace from txt file on many files?

  22. 22

    Search number from 'txt A' and replace into csv file

  23. 23

    Search txt file for varying keyword - then assign to variable

  24. 24

    Search inside txt file using javascript

  25. 25

    Python3 search for input in txt file

  26. 26

    For a given input search the .txt file and if the search found print the whole line

  27. 27

    Python Parse log file string into txt file

  28. 28

    Search file names that contain a string

  29. 29

    search and replace part of string in file

HotTag

Archive