满意答案
winters74
2017.03.31
采纳率:42% 等级:11
已帮助:1125人
#include
#include //for function exit()
#include //for functions strcpy() and strlen()
const int MAXSIZE = 200;//行最多字符数
int main() {
char line[MAXSIZE],pline[MAXSIZE];
FILE *inp;//被修改的磁盘文件
FILE *outp;//修改后的磁盘文件
char oldfile[] = "indata.txt";
char newfile[] = "outdata.txt";
if((inp = fopen(oldfile,"rt")) == NULL || (outp = fopen(newfile,"wt")) == NULL) {
printf("打开文件时出错!\n");
exit(1);
}
fscanf(inp,"%s",pline);
while(!feof(inp)) {
fscanf(inp,"%s",line);
if(strlen(line) >= 1) {//本行不是空行
fprintf(outp,"%s%s",pline,line);//上一行和本行被全部写入新文件
}
else {//本行是空行
fprintf(outp,"%s",line);//仅写入本行,上一行被丢弃
}
strcpy(pline,line);// 当前行变成了上一行
}
fclose(inp);
fclose(outp);
return 0;
}
00分享举报