0

I'm working on a c++ file and I've encountered an interesting problem. I'm outputting strings into a text file (using ofstream) and I have the following lines.

void InstructionWriter::outputLabel(string s){
string sLabel;
sLabel = s;
sLabel.erase(remove(sLabel.begin(), sLabel.end(), ' '),sLabel.end());

sLabel = "(" + function + "$" + sLabel + ")\n" ;
outputFile << sLabel;
}

The problem is during the txt file it outputs.

When I head to the text file where outputLabel was run, highlighting the line counts the characters +1 character. that +1 is "invisible." Highlighting the line won't select it. The only way to fix it is to start deleting from the right. After you hit the ')' I'll notice that I hit delete again but the cursor didn't move and it seems like nothing got deleted.

I think It's sneaking in a zero width character but I don't know how to strip that from the string, does anybody have any ideas on what functions to look into?

@smac89

terminate called after throwing an instance of 'std::length_error'
  what():  basic_string::_M_replace
0

That is what the terminal threw at me after running that command you mentioned.

Dart Feld
  • 21
  • 4

2 Answers2

0

Instead of sLabel.erase(remove(sLabel.begin(), sLabel.end(), ' '),sLabel.end()); please try this:

std::string from = " ", to = "";
size_t start_pos = 0;
while ((start_pos = sLabel.find(from, start_pos)) != std::string::npos) {
  sLabel.replace(start_pos, from.length(), to);
  start_pos += to.length();
}

Because the string is UTF-8 encoded; and You cannot rely on individual bytes. Manipulate sub-strings only.

Ali Tavakol
  • 405
  • 3
  • 11
0

Everyone, I was able to figure it out.

per Smacs comment, I uploaded the output text file to a binary editor. oddly enough, I found an 0D before the newline character that I manually put in.

I used a regex replace on the string and now it's not adding that 0D character in the string.

Thanks for all the tips.

Dart Feld
  • 21
  • 4
  • @Ali No it's not. There's no specified encoding in the C++ specification. Most likely it's ASCII encoded, as that's the usual encoding on most systems since a few decades. – Some programmer dude Dec 06 '18 at 04:50
  • Let me guess, you're on a Windows system? Then that's *normal* as the end-of-line sequence on Windows is `"\r\n"`. What you see is the carriage-return `'\r'`. And that's how text-files work, a newline is translated to the OS-specific end-of-line sequence when writing, and the opposite translation when reading. Don't remove it, unless you need to copy the file to a POSIX system (like Linux or macOS, and then there are utilities that can convert between the platforms). – Some programmer dude Dec 06 '18 at 04:52
  • So, if we have `std::string s = "سلام";` in Arabic, how do you think bytes will be? – Ali Tavakol Dec 06 '18 at 08:20