문제

I have a configuration file that is supposed to have \r\n line ending style, and I want to include the code in my program to check and correct the format. Existing code:

int convert_line_endings(FILE *fp)
{
    char c = 0, lastc = 0, cnt = 0;
    while((c = fgetc(fp)) != EOF)
    {
        if((c == '\n') && (lastc != '\r'))
        {
            cnt++;
        //somehow "insert" a '\r' in here, after the previous char and before the '\n'
        }
        lastc = c;
    }
    return cnt;
}

And in C programming, you can't "insert" a char (or can you?!), just overwrite one or the other. Any suggestions?

도움이 되었습니까?

해결책

No, you can't delete or insert. What you can do is write to a new temporary file by copying everything except \r\n sequences and then overwrite the original file with it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top