开发者

Remove control M from a text file in C

ist is possible? What would be the easiest way? I tried to compare i开发者_如何学Gon the input string character to character so

if(char([i]=="^M") char[i]=""

but it does not work.

By the way, if I were able to check it, what is the wistes substitution? to "" ?

Thanks


A control-M isn't stored as a multiple key sequence in a text file. It's generally stored as the ascii value 13, or 0x0d in hexadecimal.

So, your statement would be:

if (char[i] == 0x0d)

or

if (char[i] == '\x0d')


If you have a mutable array of char then if you need to remove a given character you'll need to move all the characters after the removed character up one place, not just assign a 'blank' to the given character.

It's probably easiest to do this with pointers.

E.g. (in place transformation):

extern char *in;
char *out = in;

while (*in)
{
    if (*in != '\r')
        *out++ = *in;

    in++;
}

*out = '\0';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜