how to read file

/* p3a processes a logfile (passed as string parameter logFilename)
* and generates a std::vector of std::string where each element is a non-empty
* line from the logfile
* The logfile may contain a number of empty lines in any position
*
* The order of the vector elements must be the same as that of the file
*/
std::vector<std::string> p3a(const std::string & logFilename);

how to solve this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
std::vector<std::string> p3a(const std::string & logFilename) {
    std::vector<std::string> result;

    //open file 
    file = open(logFileName);
    //read each line
    line = getline(file);
    while (has more lines from file) {
           //add only if line is not empty
           if (length of line not zero) {
                   result.push_back(line);
           }
            //read next line
            line = getline(file);
    }
    //close file
    close(file);
    
    //return vector
    return result;
}


Note: This is just a pseudo-code and not an actual one. Just get the idea and generate actual codes for that.
Topic archived. No new replies allowed.