【发布时间】:2012-10-26 22:17:26
【问题描述】:
我有一个应该具有 \r\n 行尾样式的配置文件,我想在我的程序中包含代码以检查和更正格式。 现有代码:
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;
}
在 C 编程中,你不能“插入”一个字符(或者你可以吗?!),只需覆盖一个或另一个。有什么建议吗?
【问题讨论】:
-
perl -pi -e 's/\n/\r\n/g' file -
呃...不,谢谢。我希望它在 C 和我的程序内部,它不应该依赖于 perl。
标签: c file-io line-endings