Question

Just a quick question. When reading in a text file, and looking for a new line character to replace with something else, could one look simply for '\n' ? or should one look for the ascii value?

what I need to do, to clarify is to find new lines in a text file and remove them if there is only one, if there are two or more in a row, i need to add a string in place of them (always one less than the amount of new lines).

I have a pretty good idea of how to do this, but i would like to know how to look for the newline character at the end of any given line to replace it?

in pseudo code (i dont believe you should need any of my actual code for a question like this) it would be:

if (line[x] & line [x+1] = new line){ replace with this } else if (line[x] = new line){ remove }

Was it helpful?

Solution

As I understand it no newlines shall be kept. Read line by line:

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

std::string s,totalString;
while (std::getline(file, s))
{
    if(s.empty())
       totalString+="added string";
    else
       totalString+=s;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top