0

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 }

Cristian C.
  • 808
  • 11
  • 34
  • `'\n'` _is_ the ASCII value. It's not at all clear what your problem is. Show a _testcase_. – Lightness Races in Orbit Mar 15 '14 at 20:15
  • @LightnessRacesinOrbit Its not a problem I am having, just a question. And i was sure the ASCII value for it is 0xA. – Cristian C. Mar 15 '14 at 20:18
  • If you use getline() to get your line from the file, the newline char is extracted and discarded, and thus, not find-able in the string. It IS possible to read the whole file into a "std::stringstream ss;" object, in one line of code. This will keep the newlines in the ss, and then you can use std::string::find() to find "\n", or "\n\n", etc. – 2785528 Mar 15 '14 at 20:25
  • @CristianC.: 0xA and `'\n'` differ only in type, not in value. In fact, that means `'\xA'` and `'\n'` are _exactly_ the same literal. `\n` is defined to mean this. Think of it as a convenient constant. – Lightness Races in Orbit Mar 15 '14 at 20:26
  • [`'\n' == 0xA`](http://ideone.com/3wNlbg) (as long as your execution character set is ASCII compatible). – Joseph Mansfield Mar 15 '14 at 20:27
  • If a 'white space only' line is found, is it empty or not? – 2785528 Mar 15 '14 at 20:28
  • @LightnessRacesinOrbit yes i understand, i was just unsure of which c++ would like me to use. – Cristian C. Mar 15 '14 at 20:29
  • As much as `'a'`, `'b'`, `'!'` and `'\n'` *look* like characters, they're not - they're integer values. The values just happen to *represent* characters according to a particular character set. So C++ doesn't care what you use as long as the value is correct. – Joseph Mansfield Mar 15 '14 at 20:31
  • @Lightness Races in Orbit: `'\n'` is a magic value that the text-mode i/o routines will translate to and from the host platform's convention for line separators. In all implementations I know, the value used for `'\n'` is the same value as an ASCII line feed, but I've never found language in the C or C++ standards that require it. In fact, the standard seems to allow for character sets like EBCDIC which don't have a line feed character. See: http://stackoverflow.com/questions/1279779/what-is-the-difference-between-r-and-n/9549183#9549183 – Adrian McCarthy Mar 15 '14 at 23:13
  • @AdrianMcCarthy: Yes, the value depends on the execution character set in use. However, this has basically nothing to do with the host platform. C++ does not care whether the OS's underlying file access layer translates typical line-endings. For C++, _it is not "magic"_. The result is precisely the same if he writes `'\xA'` rather than `'\n'`, because both are the same value (yes, I'm assuming ASCII in this sentence). That you wrote, specifically, a \ then a `n` is not relevant to that. It is not some separate, additional sentinel value with inherent magical properties. This is important. – Lightness Races in Orbit Mar 16 '14 at 02:51
  • @LightnessRacesinOrbit: Where in the C or C++ standards does it guarantee that `'\n'` is precisely the same as `'\xA'`? It's my understanding that the value the compiler uses for `'\n'` is compiler-defined, thus it is a magic value. – Adrian McCarthy Mar 16 '14 at 16:23

1 Answers1

1

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;
}
user2672165
  • 2,986
  • 19
  • 27
  • I do believe this was exactly what i was looking for, and this is what i had in mind, the only problem is that: if there is only one new line, remove it, but if there are two or more new lines, add the string. so if there are 3 new lines in a row, add two of the string. i was unsure of how to accomplish the following: if(s.empty() && s+1.empty()){ – Cristian C. Mar 15 '14 at 20:31
  • @Cristian C.: You could add a counter that is incremented each time an empty line is encountered. When you encounter non-empty line or after end of file you write as many strings you need and reset the counter. – user2672165 Mar 15 '14 at 20:41