Home > Back-end >  Delete empty lines in file
Delete empty lines in file

Time:05-15

I'm trying to delete empty lines from a file. Currently it deletes all except for the one I add at line 35:out_file << data_vector[i] << "\n";

How do I exclude the last "\n" so it doesn't add a new line?

Here's the full function:

std::vector<std::string> Data::Data::ReadData() {

    std::regex find_empty_line("^(\\s*)$");
    std::smatch match;

    std::string data, fel;
    std::vector<std::string> data_vector;

    std::ifstream in_file;
    in_file.open(file_name);

    if (in_file.is_open()) {
        while (in_file.good()) {
            std::getline(in_file, data);
            data_vector.push_back(data);
        }
    }

    in_file.close();

    std::ofstream out_file;
    out_file.open(file_name);

    if (out_file.is_open()) {
        for (int i = 0; i < data_vector.size();   i) {
            if (!data_vector[i].empty()) {
                out_file << data_vector[i] << "\n";
                std::cout << data_vector[i] << "\n";
            }
        }
    }

    out_file.close();

    return data_vector;

}

CodePudding user response:

How do I exclude the last "\n" so it doesn't add a new line?

By outputting a \n only when you have a subsequent line to also output, eg:

std::vector<std::string> Data::Data::ReadData() {

    std::vector<std::string> data_vector;

    std::ifstream in_file(file_name);
    if (in_file.is_open()) {
        std::string data;
        while (std::getline(in_file, data)) {
            data_vector.push_back(data);
        }
        in_file.close();
    }

    std::ofstream out_file(file_name);
    if (out_file.is_open()) {
        bool output_line_break = false;
        for (int i = 0; i < data_vector.size();   i) {
            if (!data_vector[i].empty()) {
                if (output_line_break) {
                    out_file << '\n';
                }
                else {
                    output_line_break = true;
                }
                out_file << data_vector[i];
                std::cout << data_vector[i] << '\n';
            }
        }
        out_file.close();
    }

    return data_vector;
}

CodePudding user response:

It's best practice (on most Operating Systems), and expected by a lot of file processing tools, to have a newline at the end of every line in a file, so you normally wouldn't want to remove the final newline... consider it a line termination character with no line after it, rather than a line separator with an implicit empty line afterwards.

But, if you really must avoid that to work with the tools you're stuck with, then one easy way to avoid outputting it for the last line is to:

out_file << data_vector[i] << (i < data_vector.size() - 1) ? "\n" : "");

If you're unfamiliar with the ternary conditional operator, the basic form is:

conditional-expression ? expression-used-if-true : expression-used-if-false

Examples: (a <= b ? a : b) evaluates to the minimum of a and b. Read more here if you need/want.

Then, to make that work, we need to make sure the last line in data_vector is the last line of output, which it might not currently be because we have some empty line in there, so let's filter those out while populating the vector (no need to check good()):

while (std::getline(in_file, data))
    if (!data.empty())
        data_vector.push_back(data);

Knowing there are no empty lines in data_vector then lets us simplify the output loop:

for (int i = 0; i < data_vector.size();   i) {
    out_file << data_vector[i]
             << (i < data_vector.size() - 1) ? "\n" : "");
    std::cout << data_vector[i] << "\n";
}

(This does assume you don't want the empty lines in the returned vector, either...)

  •  Tags:  
  • c
  • Related