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函数,去掉便可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值