Remove Carriage Returns from char* (C++)
I have defined a resource file in my Visual Studio 2008 C++ project. The problem is that after obtaining the resource data with LockResource method the resulting char buffer contains Carriage Returns and Line Feeds where as the original d开发者_开发问答ata contained only Line feeds. For instance if the original line contains:
00 0A FC 80 00 00 27 10 00 0A FC 80 00 00 27 10
The resulting char* contains also Carriage Returns (0D):
00 0D 0A FC 80 00 00 27 10 00 0D 0A FC 80 00 00
I tried the following code to get rid of them but this results in both carriage return and line feed to be removed:
for (int i = 0; i < sz; ++i)
{
// Ignore carriage returns
if (data[i] == '\n') continue;
// ...
}
How do I get rid of Carriage Returns but leave New Line characters?
EDIT:
To make it more specific. I am writing the char buffer into the file:
std::ofstream outputFile(fileName.c_str());
for (int i = 0; i < sz; ++i)
{
// Ignore carriage returns
// if (data[i] == '\r') continue; This leaves both CR and LF
// if (data[i] == 0x0D) continue; This leaves both CR and LF
if (data[i]) == '\n') continue; //This removes both CR and LF
outputFile << data[i];
}
When you write a '\n' to a text file it is converted into the 'End of Line Sequence'. In your case this looks like '\r\n'. When you read from a text file the 'End of Line Sequence' is converted back into a single '\n' character. So for normal processing this extra character should not affect you (assuming you open both read/write as text files).
If you do not want your '\n' converted into the 'End of Line Sequence' then open the file as binary. When this is done then no processing is done on the '\n' character.
std::ofstream outputFile(fileName.c_str(), std::ios::binary);
// ^^^^^^^^^^^^^^^^
But note: The file representation should not affect you. If you open and read the file as text the '\r\n' will be converted back into a single '\n' character within your application.
Here's an example of converting it in-place. Variable i
is the index of the character being read, and variable j
is the index of the character being written. As you pass by carriage returns, i
will begin to "overtake" j
:
#include <stdio.h>
int main(void)
{
char s[100] = "This is a test.\n\rThis is only a test.\n\r";
for(int i = 0, j = 0; s[i] != '\0'; i++)
if(s[i] != '\r')
s[j++] = s[i];
printf("%s", s);
}
Use '\r'
instead of '\n'
in the given code?
The char '\n'
corresponds to the line feed, 0x0A
.
'\r'
corresponds to the carriage return, 0x0D
. If you only want to remove the carriage return, use that.
Your code will not remove both, by the way; it will only remove the line feed and leave the carriage return in place.
Furthermore, there is no such thing as a “new line character” on Windows because Windows (unlike other operating systems) conventionally uses a two-character sequence to signify a line break: "\r\n"
. This is also the reason why loading the resource changes the byte sequence: Windows thinks that you are loading a text and automatically converts all possible line breaks to the canonical form. To prevent this, don’t load the resource as a text but rather as a raw byte sequence (if that’s possible).
Finally, depending on how you display the string other character sequences will also result in a line break. For example, all modern editors (this means: not notepad) will accept any newline sequence to create a line break, be it '\r'
, '\n'
or "\r\n"
.
All you have to do is open the file in binary mode. Like this:
std::ofstream outputFile(fileName.c_str(), std::ios::out | std::ios::binary);
精彩评论