当前位置:网站首页>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
边栏推荐
- Thoroughly understand the point cloud processing tutorial based on open3d!
- 学习八股文的知识点~~1
- 高频面试题
- 鸿蒙第四次学习
- R语言dplyr包na_if函数把向量数值中的控制转化为缺失值NA、按照映射规则把指定内容转化为缺失值NA
- After 22 years in office, the father of PowerShell will leave Microsoft: he was demoted by Microsoft for developing PowerShell
- 从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
- How to copy and paste interlaced in Excel
- R language uses the lsnofunction function function of epidisplay package to list all objects in the current space, except user-defined function objects
- Crypto usage in nodejs
猜你喜欢
开源物联网平台ThingsBoard的安装
第一次去曼谷旅游怎么玩?这份省钱攻略请收好
A simple PHP personal card issuing program v4.0
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
Excel如何进行隔行复制粘贴
Stratégie touristique d'été de Singapour: un jour pour visiter l'île de San taosha à Singapour
ICDE 2023|TKDE Poster Session(CFP)
新加坡暑假旅遊攻略:一天玩轉新加坡聖淘沙島
思维意识转变是施工企业数字化转型成败的关键
Industrial software lecture - core technology analysis of 3D CAD design software - the second lecture of the Forum
随机推荐
What is cloud primordial? This time, I can finally understand!
R语言dplyr包rowwise函数、mutate函数计算dataframe数据中多个数据列在每行的最大值、并生成行最大值对应的数据列(row maximum)
What are the links of the problem
Ali was wildly asked by the interviewer on three sides. Redis dared not write 'proficient' on his resume anymore
The difference between promise and observable
性能测试如何创造业务价值
R language ggplot2 visual Facet: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
新手必看,點擊兩個按鈕切換至不同的內容
Hospital online inquiry source code hospital video inquiry source code hospital applet source code
[test development] software testing - concept
@Component cannot get Dao layer
R语言使用epiDisplay包的lrtest函数对多个glm模型(logisti回归)执行似然比检验(Likelihood ratio test)对比两个模型的性能是否有差异、广义线性模型的似然比检
新加坡暑假旅遊攻略:一天玩轉新加坡聖淘沙島
Excel查找一列中的相同值,删除该行或替换为空值
R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
Web实时通信技术之Websocket
How to set vscode to delete the whole line shortcut key?
How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
《病人家属,请来一下》读书笔记
Progress-进度条