源文件为read.txt,处理后文件为write.txt(每行字符数不能超过1024个)
[C] 纯文本查看 复制代码 #include <stdio.h>
#include <string.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning (disable: 4996)
int main()
{
FILE* fp;
FILE* fd;
char rowNum[5];
char buf[1024];
char newbuf[1024];
int i;
//读取文件
fp = fopen("read.txt", "r");
if (fp == NULL)
{
printf("文件读取失败\n");
return -1;
}
else
{
printf("正在读取文件...\n");
}
//写入文件
fd = fopen("write.txt", "w");
if (fd == NULL)
{
printf("写入文件打开失败\n");
return -2;
}
else
{
printf("正在写入文件...\n");
}
//读取每一行
while (fgets(buf, sizeof(buf), fp) != NULL)
{
//读取行号
strncpy(rowNum, buf, 4);
rowNum[4] = '\0';
//判断是否要换行
for (i = 5; buf[i] != '\0'; i++)
{
if (buf[i] == '!' && buf[i + 1] == '2' && buf[i + 2] == ':')
break;
}
if (buf[i] != '\0')
{
strncpy(newbuf, buf, i + 1);
newbuf[i + 1] = '\n';
newbuf[i + 2] = '\0';
strcat(newbuf, rowNum);
strcat(newbuf, " ");
strcat(newbuf, buf + i + 1);
fputs(newbuf, fd);
}
else
{
fputs(buf, fd);
}
//清空buf
memset(buf, 0, sizeof(buf));
}
//关闭
fclose(fp);
fclose(fd);
return 0;
}
源码及程序(win64):
链接: https://pan.baidu.com/s/1SilvekavrhzW2i6Rxdmmjw 提取码: 4d4b |