当前位置:网站首页>C file input operation
C file input operation
2022-07-02 19:11:00 【Leisurely young heart】
Copy file
// Copy file
#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 the source file is not empty Read this file
{
if ((f_dest = fopen(dest, "wb")) != NULL) // If the target file is not empty Writing documents
{
//1) buffer Is the address to receive data , about fread To read the address of data , That is, the address where the data is saved
//2) size Is the number of single bytes of content to be read .
//3) count Is it necessary to read out size The number of data items in bytes .
//4) stream Pointer to the target file .
while((file_size=fread(buf,sizeof(char),sizeof(buf),f_src))>0)//fread The function is used to read data from a file stream , Its function prototype is :size_t fread(void* buffer, size_t size, size_t count, FILE*stream);
{
fwrite(buf, file_size, sizeof(char), f_dest);// Write data to f_dest This flow
memset(buf,0,sizeof(buf));// Clear data
}
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");// Delete file
system("pause");
return 0;
}
File read and write
#pragma warning(disable:4996)
#if 0
#include<stdio.h>
#include<stdlib.h>
#include<errno.h >
#include <string.h>
int main() {
// The latter mode is adjustable
//w( Just write )
//r( read-only )
//a( additional Add you after the previous content fprintf What's in it )
//w+ r+ a+ // All read and write
//r+b // Read binary
//w+b // Binary write
#if 0
FILE* p = fopen("D:/fileTest/flieopen.txt", "w");//D:\fileTest The path is copied like this \ Change it into / perhaps \\ // Note that it is not safe to use this method forcibly. Please add #pragma warning(disable:4996)
fprintf(p, "55555 \n");
fclose(p);
#endif // 0
#if 0
// Read the file
// The first parameter path The second is format r Indicates read-only w write in
FILE* p = fopen("D:/fileTest/flieopen.txt", "r");//D:\fileTest The path is copied like this \ Change it into / perhaps \\ // Note that it is not safe to use this method forcibly. Please add #pragma warning(disable:4996)
char buff[256];
fgets(buff, 255, p);// Parameters :buff buff Size p object fgets Get array fgetc Character acquisition
printf("%s", buff);
fclose(p);
#endif // 0
#if 0
// Read the file
// The first parameter path The second is format r Indicates read-only w write in
FILE* p = fopen("D:/fileTest/flieopen.txt", "w");//D:\fileTest The path is copied like this \ Change it into / perhaps \\ // Note that it is not safe to use this method forcibly. Please add #pragma warning(disable:4996)
fprintf(p, "55555 \n");
fseek(p,2,SEEK_SET);// The cursor
fprintf(p, "666 \n");// It is equivalent to replacing the three digits behind the cursor with 666
fclose(p);
#endif // 0
// Error handling If opening the file fails
FILE* p = fopen("D:/fileTest/flieopen.txt", "w");
if (!p)
{
int ErrorCode = errno;
fprintf(stderr, " The error code is : %d", ErrorCode);//stderr Is a global error flow
fprintf(stderr," error message : %s\n",strerror(ErrorCode));
system("pause");
exit(-1); //EXIT_FAILURE EXIT_SUCCESS
}
fclose(p);
system("pause");
exit(EXIT_SUCCESS);
return 0;
}
#endif // 0
边栏推荐
- R language uses the lsnofunction function function of epidisplay package to list all objects in the current space, except user-defined function objects
- 论文导读 | 机器学习在数据库基数估计中的应用
- Hongmeng's fourth learning
- What are the links of the problem
- R language dplyr package Na_ The if function converts the control in the vector value into the missing value Na, and converts the specified content into the missing value Na according to the mapping r
- Have you stepped on the nine common pits in the e-commerce system?
- Progress-进度条
- Crypto usage in nodejs
- Mysql高级篇学习总结8:InnoDB数据存储结构页的概述、页的内部结构、行格式
- ORA-01455: converting column overflows integer datatype
猜你喜欢

PyTorch函数中的__call__和forward函数

Tips for material UV masking

Imitation Jingdong magnifying glass effect (pink teacher version)

Thoroughly understand the point cloud processing tutorial based on open3d!

Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比

MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree

开发固定资产管理系统,开发固定资产管理系统用什么语音

Hongmeng's fourth learning

How performance testing creates business value
![[daily question] first day](/img/8c/f25cddb6ca86d44538c976fae13c6e.png)
[daily question] first day
随机推荐
Stratégie touristique d'été de Singapour: un jour pour visiter l'île de San taosha à Singapour
2022软件工程期末考试 回忆版
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
Stm32g0 USB DFU upgrade verification error -2
开源物联网平台ThingsBoard的安装
Excel finds the same value in a column, deletes the row or replaces it with a blank value
论文导读 | 机器学习在数据库基数估计中的应用
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
juypter notebook 修改默认打开文件夹以及默认浏览器
How to delete the border of links in IE? [repeat] - how to remove borders around links in IE? [duplicate]
R language uses Cox of epidisplay package Display function obtains the summary statistical information of Cox regression model (risk rate HR, adjusted risk rate and its confidence interval, P value of
页面标题组件
metric_logger小解
思维意识转变是施工企业数字化转型成败的关键
Progress progress bar
产品经理应具备的能力
Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比
Crypto usage in nodejs
codeforces每日5题(均1700)-第四天
【JVM调优实战100例】01——JVM的介绍与程序计数器