当前位置:网站首页>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
边栏推荐
- 虚拟机初始化脚本, 虚拟机相互免秘钥
- 使用CLion编译OGLPG-9th-Edition源码
- How to enable the run dashboard function of idea
- R language ggplot2 visualization: gganimate package creates dynamic histogram animation (GIF) and uses transition_ The States function displays a histogram step by step along a given dimension in the
- R language uses lrtest function of epidisplay package to perform likelihood ratio test on multiple GLM models (logisti regression). Compare whether the performance of the two models is different, and
- 高级性能测试系列《24. 通过jdbc执行sql脚本》
- 聊聊电商系统中红包活动设计
- MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
- 使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
- What is 9D movie like? (+ common sense of dimension space)
猜你喜欢

How to play when you travel to Bangkok for the first time? Please keep this money saving strategy

【每日一题】第一天

Excel finds the same value in a column, deletes the row or replaces it with a blank value

Looking for innocence in New York -- a beautiful day at the discovery center of Legoland, New Jersey

【测试开发】软件测试—概念篇

Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
![[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning](/img/59/6c776e0607a52962b72fbea2e64c8e.png)
[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning

数据降维——主成分分析

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

Learning summary of MySQL advanced 6: concept and understanding of index, detailed explanation of b+ tree generation process, comparison between MyISAM and InnoDB
随机推荐
PHP-Parser羽毛球预约小程序开发require线上系统
R语言使用epiDisplay包的cox.display函数获取cox回归模型汇总统计信息(风险率HR、调整风险率及其置信区间、模型系数的t检验的p值、Wald检验的p值和似然比检验的p值)、汇总统计
Golang concurrent programming goroutine, channel, sync
新加坡暑假旅遊攻略:一天玩轉新加坡聖淘沙島
数据降维——因子分析
Mysql高级篇学习总结8:InnoDB数据存储结构页的概述、页的内部结构、行格式
Looking for innocence in New York -- a beautiful day at the discovery center of Legoland, New Jersey
[0701] [paper reading] allowing data imbalance issue with perforated input during influence
Crypto usage in nodejs
LightGroupButton* sender = static_cast<LightGroupButton*>(QObject::sender());
Fastdfs installation
ICDE 2023|TKDE Poster Session(CFP)
juypter notebook 修改默认打开文件夹以及默认浏览器
What is 9D movie like? (+ common sense of dimension space)
R language uses lrtest function of epidisplay package to perform likelihood ratio test on multiple GLM models (logisti regression). Compare whether the performance of the two models is different, and
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
Novice must see, click two buttons to switch to different content
[fluent] dart data type (VaR data type | object data type)
【JVM调优实战100例】03——JVM堆调优四例