当前位置:网站首页>C文件输入操作
C文件输入操作
2022-07-02 17:50:00 【悠然少年心】
复制文件
//复制文件
#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#include<errno.h >
#include <string.h>
#include<stdbool.h>
bool copy_file(const char *src, const char* dest)
{
char buf[512] = {
0 };
int file_size = 0;
FILE* f_src = NULL;
FILE* f_dest = NULL;
if ((f_src = fopen(src, "rb")) != NULL)//如果源文件不为空 读这个文件
{
if ((f_dest = fopen(dest, "wb")) != NULL) //如果目标文件不为空 写文件
{
//1) buffer为接收数据的地址,对于fread来说是要读出数据的地址,即数据保存的地址
//2) size是要读出内容的单字节数。
//3) count是要进行读出size字节的数据项的个数。
//4) stream为目标文件指针。
while((file_size=fread(buf,sizeof(char),sizeof(buf),f_src))>0)//fread函数用于从文件流中读取数据,其函数原型为:size_t fread(void* buffer, size_t size, size_t count, FILE*stream);
{
fwrite(buf, file_size, sizeof(char), f_dest);//将数据写入f_dest这个流
memset(buf,0,sizeof(buf));//清除数据
}
fclose(f_dest);
fclose(f_src);
return true;
}
}
return false;
}
int main() {
copy_file("D:\\fileTest\\233.txt", "D:\\fileTest\\233233.txt");
//remove("D:\\fileTest\\233233.txt");//删除文件
system("pause");
return 0;
}
文件读写
#pragma warning(disable:4996)
#if 0
#include<stdio.h>
#include<stdlib.h>
#include<errno.h >
#include <string.h>
int main() {
//后面这个模式是可以调的
//w(只写)
//r(只读)
//a(附加 在前面的内容后面附加你fprintf里面的内容)
//w+ r+ a+ //都是读写
//r+b //二进制读
//w+b //二进制写
#if 0
FILE* p = fopen("D:/fileTest/flieopen.txt", "w");//D:\fileTest路径复制出来是这样 \要换成/或者\\ //注意使用这种方法不安全强行使用请在前面添加#pragma warning(disable:4996)
fprintf(p, "55555 \n");
fclose(p);
#endif // 0
#if 0
//读取文件
//第一个参数路径 第二个是格式 r表示只读 w写入
FILE* p = fopen("D:/fileTest/flieopen.txt", "r");//D:\fileTest路径复制出来是这样 \要换成/或者\\ //注意使用这种方法不安全强行使用请在前面添加#pragma warning(disable:4996)
char buff[256];
fgets(buff, 255, p);//参数:buff buff的大小 p对象 fgets获取数组fgetc获取字符
printf("%s", buff);
fclose(p);
#endif // 0
#if 0
//读取文件
//第一个参数路径 第二个是格式 r表示只读 w写入
FILE* p = fopen("D:/fileTest/flieopen.txt", "w");//D:\fileTest路径复制出来是这样 \要换成/或者\\ //注意使用这种方法不安全强行使用请在前面添加#pragma warning(disable:4996)
fprintf(p, "55555 \n");
fseek(p,2,SEEK_SET);//游标
fprintf(p, "666 \n");//相当于是替换游标后面的三位为666
fclose(p);
#endif // 0
//错误处理 如果打开文件失败
FILE* p = fopen("D:/fileTest/flieopen.txt", "w");
if (!p)
{
int ErrorCode = errno;
fprintf(stderr, "错误代码是: %d", ErrorCode);//stderr是一个全局的错误流
fprintf(stderr,"错误信息: %s\n",strerror(ErrorCode));
system("pause");
exit(-1); //EXIT_FAILURE EXIT_SUCCESS
}
fclose(p);
system("pause");
exit(EXIT_SUCCESS);
return 0;
}
#endif // 0
边栏推荐
- The difference between promise and observable
- Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比
- 故障排查:kubectl报错ValidationError: unknown field \u00a0
- Deep learning mathematics foundation
- Which securities company has a low, safe and reliable online account opening commission
- Looking for innocence in New York -- a beautiful day at the discovery center of Legoland, New Jersey
- M2DGR:多源多场景 地面机器人SLAM数据集(ICRA 2022 )
- Introduction to the paper | application of machine learning in database cardinality estimation
- sql训练2
- 昨天阿里学长写了一个责任链模式,竟然出现了无数个bug
猜你喜欢

Responses of different people in technology companies to bugs | daily anecdotes
![[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter](/img/c4/3bba96fda92328704c2ddd929dcdf6.png)
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter

Installation of thingsboard, an open source IOT platform

高频面试题

How can retail enterprises open the second growth curve under the full link digital transformation

How to enable the run dashboard function of idea

在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天

【JVM调优实战100例】01——JVM的介绍与程序计数器

性能测试如何创造业务价值

MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
随机推荐
The difference between SLC, MLC, TLC and QLC NAND SSD: which is better?
全链路数字化转型下,零售企业如何打开第二增长曲线
Stretchdibits function
Learn the knowledge points of eight part essay ~ ~ 1
SLAM|如何时间戳对齐?
《病人家属,请来一下》读书笔记
[fluent] dart data type (VaR data type | object data type)
How can retail enterprises open the second growth curve under the full link digital transformation
The text editor hopes to mark the wrong sentences in red, and the text editor uses markdown
高频面试题
After 22 years in office, the father of PowerShell will leave Microsoft: he was demoted by Microsoft for developing PowerShell
Deep learning mathematics foundation
SQL training 2
思维意识转变是施工企业数字化转型成败的关键
论文导读 | 机器学习在数据库基数估计中的应用
How to delete the border of links in IE? [repeat] - how to remove borders around links in IE? [duplicate]
鸿蒙第四次学习
迷你高尔夫球场:伦敦休闲旅游好去处
从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction