Method to find string inside of the text file. Then getting the following lines up to a certain limit

前端 未结 7 559
孤街浪徒
孤街浪徒 2020-12-08 15:35

So this is what I have so far :

public String[] findStudentInfo(String studentNumber) {
                Student student = new Student();
                Scan         


        
相关标签:
7条回答
  • 2020-12-08 16:34

    I am doing something similar but in C++. What you need to do is read the lines in one at a time and parse them (go over the words one by one). I have an outter loop that goes over all the lines and inside that is another loop that goes over all the words. Once the word you need is found, just exit the loop and return a counter or whatever you want.

    This is my code. It basically parses out all the words and adds them to the "index". The line that word was in is then added to a vector and used to reference the line (contains the name of the file, the entire line and the line number) from the indexed words.

    ifstream txtFile;
    txtFile.open(path, ifstream::in);
    char line[200];
    //if path is valid AND is not already in the list then add it
    if(txtFile.is_open() && (find(textFilePaths.begin(), textFilePaths.end(), path) == textFilePaths.end())) //the path is valid
    {
        //Add the path to the list of file paths
        textFilePaths.push_back(path);
        int lineNumber = 1;
        while(!txtFile.eof())
        {
            txtFile.getline(line, 200);
            Line * ln = new Line(line, path, lineNumber);
            lineNumber++;
            myList.push_back(ln);
            vector<string> words = lineParser(ln);
            for(unsigned int i = 0; i < words.size(); i++)
            {
                index->addWord(words[i], ln);
            }
        }
        result = true;
    }
    
    0 讨论(0)
提交回复
热议问题