Home > Software engineering >  Issues reading text from a file. Getting double reads
Issues reading text from a file. Getting double reads

Time:11-23

Hello friends at stackoverflow!

I have written a program that saves 3 strings to a textfile. The code to write is:

void my_class::save_file(const char* text) {
    for(auto ad: ad_list) {
        std::ofstream outputFile;
        outputFile.open(text, std::ios::app);
        if (outputFile.is_open()) {
            outputFile << ad.string1 << "|" << ad.string2 << "|" << ad.string3 << "|" << std::endl;
            outputFile.close();
        }
    }
}

This gives me the file with the content:

string1|string2|string3|
   <- //extra line here

When i read from this file I do it with the following function:

void my_class::read_file(const char* text) {
    // read file to stringsteam
    std::stringstream ss;
    std::ifstream fp (text);
    if (fp.is_open()) {
        ss << fp.rdbuf(); 
    }
    fp.close();
    string file_contents = ss.str();

    // split to lines
    auto lines = splitString(file_contents, "\n");

    // split to parts
    for (auto ad_text: lines) {   // <-- this is the possibly the issue
        auto ad_parts = splitString(ad_text, "|");
        string string1 = ad_parts[0];
        string string2 = ad_parts[1];
        string string3 = ad_parts[2];
        auto new_class = MyClass(string1, string2, string3);
        //Adds (push_back) the class to its vector
        add_class(new_class);
    }
}

vector<string> my_class::splitString(string text, string delimiter) {
    vector<string> parts;
    size_t start = 0;
    size_t end = 0;
    while((end = text.find(delimiter, start)) != std::string::npos) {
        size_t length = end - start;
        parts.push_back(text.substr(start, length));
        start = end   delimiter.length();
    }
    start = end   delimiter.length();
    size_t length = text.length() - start;
    parts.push_back(text.substr(start, length));

    return parts;
}

Result:

string1|string2|string3|
string1|string2|string3|

I'm pretty sure this copy is a result of the new line left behind by the write function. The read function splits the text into lines, and that would be 2 lines. I am currently splitting the lines in this loop "for (auto ad_text: lines)", and what I want to do is basically ( lines - 1 ). I do not understand how I can do that with the way I am interating through the for loop.

Thanks in advance!

CodePudding user response:

You can simplify your splitString function to look like below. Note the second parameter to splitString is a char now and not a std::string.

//note the second parameter is a char and not a string now 
vector<string> splitString(string text, char delimiter) {
    vector<string> parts;
    std::string words;
    std::istringstream ss(text);
    while(std::getline(ss, words,delimiter ))
    {
         parts.push_back(words);
    }
   
    return parts;
}

For the above modification to work you have to make 2 additional changes:

Change 1: Replace auto lines = splitString(file_contents, "\n"); to:

auto lines = splitString(file_contents, '\n');

Change 2: Replace auto ad_parts = splitString(ad_text, "|"); to:

 auto ad_parts = splitString(ad_text, '|');
  • Related