天天看点

c语言空格换行,关于文件操作,碰到空格就换行

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

#include

int main()

{

int i, off_set = 0;

char file_to_open[81];

char lines[1024], new_string[102400];

FILE *fp_read, *fp_write;

memset(file_to_open, 0x00, sizeof(file_to_open));

printf("Please input the files' full path:\n");

gets(file_to_open);

fp_read = fopen(file_to_open, "r");

if(NULL == fp_read)

{

printf("You've input a wrong path!\nProgramme will exit...\n");

sleep(3);

exit(1);

}

memset(new_string, 0x00, sizeof(new_string));

while(!feof(fp_read))

{

static int space_count = 0;

memset(lines, 0x00, sizeof(lines));

if(!fgets(lines, 1024, fp_read))

break;

printf("Now !");

for(i = 0; i < strlen(lines); i++)

{

if(lines[i] == 0x20)

{

lines[i] = '\n';

space_count ++;

}

}

strncpy(new_string + off_set, lines, strlen(lines));

off_set += strlen(lines);

}

printf(new_string);

fp_write = fopen("new_txt.txt", "w");

if(NULL == fp_write)

{

printf("Failed to open the new file!\n");

exit(1);

}

fprintf(fp_write, "%s", new_string);

printf("新文件写入成功!\n");

return 0;

}

linux gcc 编译成功并已测试,没暂时没发现bug。有问题继续问。

如果你是在windows下跑这个代码遇到错误,可能是中间有个sleep函数,去掉便可。