Remove carriage return from char * (C ++)

I defined a resource file in my Visual Studio 2008 C ++ project. The problem is that after receiving the resource data using the LockResource method, the resulting char buffer contains carriage returns and linear channels, where the source data contains only rows. For example, if the source string contains:

00 0A FC 80 00 00 27 10 00 0A FC 80 00 00 27 10

The resulting char * also contains a carriage return (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 will result in a carriage return and line feed:

for (int i = 0; i < sz; ++i) { // Ignore carriage returns if (data[i] == '\n') continue; // ... } 

How can I get rid of carriage returns, but leave newlines?

EDIT:

Make it more specific. I am writing a char buffer to a 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]; } 
+4
source share
5 answers

When you write "\ n" to a text file, it is converted to "End of line sequence". In your case, it looks like "\ r \ n". When you read from a text file, the "End of Line Sequence" is converted back to a single character "\ n". Therefore, for normal processing, this extra character should not affect you (provided that you open both reading and text files).

If you do not want your "\ n" to be converted to "End of line sequence", open the file as binary. When this is done, processing will not be performed by the character '\ n'.

 std::ofstream outputFile(fileName.c_str(), std::ios::binary); // ^^^^^^^^^^^^^^^^ 

But note : the presentation of the file should not affect you. If you open and read the file as text, "\ r \ n" will be converted back to a single character "\ n" in your application.

+6
source

Here is an example of converting it into place. The variable i is the index of the character being read, and the variable j is the index of the character being written. When you pass on the carriage return, i will start 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); } 
+4
source

Use '\r' instead of '\n' in this code?

+2
source

char '\n' matches the string, 0x0A . '\r' corresponds to carriage return, 0x0D . If you want to remove only carriage returns, use this.

Your code will not delete both, by the way; it will only remove the feed line and leave the carriage in place.

In addition, there is no such thing as a “new line character” on Windows, since Windows (unlike other operating systems) usually uses a two-character sequence to indicate a line break: "\r\n" . This is also the reason why loading a resource changes the sequence of bytes: Windows thinks that you load the text and automatically convert all possible line breaks to canonical form. To prevent this, do not load the resource as text, but rather as an unprocessed sequence of bytes (if possible).

Finally, depending on how you show the string, other character sequences also lead to line breaks. For example, all modern editors (this means: not notepad) will accept any new string sequence to create a line break, be it '\r' , '\n' or "\r\n" .

+2
source

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); 
+2
source

All Articles