当前位置:网站首页>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
边栏推荐
- Markdown基础语法
- 全志A33使用主线U-Boot
- Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
- 数据降维——主成分分析
- M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
- Golang并发编程——goroutine、channel、sync
- Tips for material UV masking
- STM32G0 USB DFU 升级校验出错-2
- MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
- 使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
猜你喜欢

数据降维——因子分析

深度学习数学基础

Hongmeng's fourth learning

How to clean up discarded PVs and their corresponding folders

Yolov3 trains its own data set to generate train txt

Deep learning mathematics foundation

Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent

Troubleshooting: kubectl reports an error validationerror: unknown field \u00a0
![[0701] [paper reading] allowing data imbalance issue with perforated input during influence](/img/c7/9b7dc4b4bda4ecfe07aec1367fe059.png)
[0701] [paper reading] allowing data imbalance issue with perforated input during influence

Excel查找一列中的相同值,删除该行或替换为空值
随机推荐
在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天
Learn the knowledge points of eight part essay ~ ~ 1
R语言使用epiDisplay包的lrtest函数对多个glm模型(logisti回归)执行似然比检验(Likelihood ratio test)对比两个模型的性能是否有差异、广义线性模型的似然比检
STM32G0 USB DFU 升级校验出错-2
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
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
How to copy and paste interlaced in Excel
2022软件工程期末考试 回忆版
Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比
仿京东放大镜效果(pink老师版)
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
The difference between SLC, MLC, TLC and QLC NAND SSD: which is better?
The student Tiktok publicized that his alma mater was roast about "reducing the seal of enrollment". Netizen: hahahahahahahaha
预处理和预处理宏
R language ggplot2 visualization: visualize the line chart and add customized X-axis label information to the line chart using labs function
高频面试题
Compile oglpg-9th-edition source code with clion
[test development] software testing - concept
产品经理应具备的能力
What are the links of the problem