c - 如何使用带有(“r”和“w”)的fopen将一个.txt转移到另一个,并将“”替换为“\n”
问题描述
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void foo(const char *file1, char *file2)
{
FILE *file = fopen(file1, "r");
FILE *file0 = fopen (file2, "w");
char nr = '\n';
int c;
fclose(file);
fclose (file0);
}
int main(){
const char t1[]="test.txt";
const char t2[]="test1.txt";
foo(t1,t2);
return 0;
}
如何在用换行符替换分隔符时读取第一个文件并复制到新文件?
解决方案
#include <stdio.h>
#include <stdlib.h>
void foo(const char *file1, const char *file2) {
FILE *fp1;
FILE *fp2;
int c;
fp1 = fopen(file1,"r");
fp2 = fopen(file2,"w+");
while(1) {
c = fgetc(fp1);
if(feof(fp1)) {
break;
}
if(c == ' ')
c='\n';
fprintf(fp2, "%c", c);
}
fclose(fp1);
fclose(fp2);
}
int main(){
const char t1[]="test.txt";
const char t2[]="test1.txt";
foo(&(t1[0]),&(t2[0]));
return 0;
}
这应该工作
推荐阅读
- azure-devops - Azure DevOps 组织管理 - 一个大型项目?
- java - WebFlux WebClient 不发送证书
- html - Shopify [液体] - 基于天的横幅的自定义框
- typescript - 使 Vuex 状态只能通过 getter 访问
- python - 最快的方法:拆分字符串并获取最后一个元素
- python - 运行异步 Elasticsearch 查询
- android - ScrollView 不使用 ConstraintLayout 滚动
- javascript - 当我在反应应用图片中使用反应路由器的重定向时,Express throws 路径必须是绝对路径或指定根到 res.sendFile
- python - 无法使用python访问json文件中的对象
- php - 使用 Pear 提交电子邮件表单时出现未定义错误