用C语言删除空行(上一行)。

Python021

用C语言删除空行(上一行)。,第1张

#include <stdio.h>

#include <stdlib.h>//for function exit()

#include <string.h>//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

}

可以使用gotoxy函数。

原型:extern void gotoxy(int x, int y)

用法:#include <system.h>

功能:将光标移动到指定位置说明:gotoxy(x,y)将光标移动到指定行y和列x。设置光标到文本屏幕的指定位置,其中参数x,y为文本屏幕的坐标。

假设上一行是屏幕的左上角。

gotoxy(0,0)//将光标移动到屏幕左上角。