文件的创建与删除,打开与关闭(C)
来源:互联网 发布:两组数据的对比 编辑:程序博客网 时间:2024/05/17 08:50
#include<stdio.h>
int main()
{
char year[10],month[5],day[5];
printf("输入年、月、日:\n");
gets(year);
gets(month);
gets(day);
//保存用fwrite()来完成,创建用fopen()来完成
char file_name [256] = {0};
printf("输入要建立的文件名:");//D:\workplace_c\c_operate\create_file.c
gets(file_name);
FILE *fp=fopen(file_name,"at+");//创建用fopen()
if(fp==NULL)
{
printf("can't create file!");
return 0;
}
fprintf(fp,"%s年%s月%s日",year,month,day);
fclose(fp);
return 0;
}
#创建
#include <stdio.h>
#include <direct.h> //用 _mkdir()创建一个文件夹(目录)
int main()
{
_mkdir("D://workplace_c//c_operate//test"); //用 _mkdir()创建一个文件夹(目录)
char c;
//FILE *fin, *fout;
FILE *fin = fopen("D://workplace_c//c_operate//test//a.txt","at+");//创建一个文件
FILE *fout;
if(fin == NULL) {
printf("I'm so sorry,Open file error.\n");
}
if((fout = fopen("D://workplace_c//c_operate//test//a.txt","w")) == NULL) {
//以 w 方式,只能用于往文件中写,如果不存在,则以指定的文件名新建一个文件
printf("I'm so sorry,Open file error.\n");
}
while((c = fgetc(fin)) != EOF)
fputc(c,fout);
fclose(fin);//关闭
fclose(fout);
return 0;
}
#删除
#include <stdio.h>
int main()
{
if(remove("D://workplace_c//c_operate//test//a.txt")){
printf("Could not delete the file &s \n","1.txt");
}else{
printf("OK \n");
}
return 0;
}
###########################文件的读写
写文件
#include <stdio.h>
int main()
{
int num;
char name[5];
FILE *fp;
if ((fp = fopen("E://workplace_c//test//a.txt", "w")) == NULL)
printf("cannot open file!\n");
scanf("%d %s",&num,name);
fprintf(fp, "%d %s", num, name);
fflush(fp); //数据刷新 数据立即更新
fclose(fp);
if ((fp = fopen("E://workplace_c//test//a.txt", "w")) == NULL)
printf("cannot open file!\n");
fwrite(&num, sizeof(int), 1, fp);
fwrite(name, sizeof(char), 5, fp);
fflush(fp); //数据刷新 数据立即更新
fclose(fp);
}
写int数据写到文件
#include <stdio.h>
#include <stdlib.h>
int main ()
{
//可以先sprintf(str,"%f",i),转成字符串,将str用fwrite写入文件。但这样再去读这些文件,需要再次将字符串转换成数字
FILE * pFile;
int buffer[] = {1, 2, 3, 4};
//b是以二进制形式写的,所以打开肯定是乱码形式
if((pFile = fopen ("E://workplace_c//test//a.txt", "wb"))==NULL)
{
printf("cant open the file");
exit(0);
}
//可以写多个连续的数据(这里一次写4个)
fwrite (buffer , sizeof(int), 4, pFile);
fclose (pFile);
return 0;
}
读取int数据
#include <stdio.h>
#include <stdlib.h>
int main () {
FILE * fp;
int buffer[4];
if((fp=fopen("E://workplace_c//test//a.txt","rb"))==NULL)
{
printf("cant open the file");
exit(0);
}
if(fread(buffer,sizeof(int),4,fp)!=4) //可以一次读取
{
printf("file read error\n");
exit(0);
}
for(int i=0;i<4;i++)
printf("%d\n",buffer[i]);
return 0;
}
#########写结构体数据到文件
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct{
int age;
char name[30];
}people;
int main ()
{
FILE * pFile;
int i;
people per[3];
per[0].age=20;strcpy(per[0].name,"li");
per[1].age=18;strcpy(per[1].name,"wang");
per[2].age=21;strcpy(per[2].name,"zhang");
if((pFile = fopen ("E://workplace_c//test//a.txt", "wb"))==NULL)
{
printf("cant open the file");
exit(0);
}
for(i=0;i<3;i++)
{
if(fwrite(&per[i],sizeof(people),1,pFile)!=1)
printf("file write error\n");
}
fflush(pFile);
fclose (pFile);
return 0;
}
读结构体数据
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct{
int age;
char name[30];
}people;
int main () {
FILE * fp;
people per;
if((fp=fopen("E://workplace_c//test//a.txt","rb"))==NULL)
{
printf("cant open the file");
exit(0);
}
while(fread(&per,sizeof(people),1,fp)==1) //如果读到数据,就显示;否则退出
{
printf("%d %s\n",per.age,per.name);
}
fclose (fp);
return 0;
}
C语言中将结构体写入且读文件- http://blog.csdn.net/mbshqqb/article/details/52825696
- 文件的创建与删除,打开与关闭(C)
- 文件的创建,打开与关闭
- C的文件打开关闭与读写
- C语言文件的打开与关闭
- C语言文件的打开与关闭
- C语言读写文件大全 之 文件的创建、打开与关闭
- 第二章 文件的创建、打开与关闭
- 文件的打开与关闭
- 文件的打开与关闭
- 文件的打开与关闭
- 【C语言】【unix c】文件的打开与文件的关闭(系统调用函数)
- C语言文件的打开与关闭_1
- 121 C语言文件的打开与关闭
- 【文件管理】文件的打开与关闭
- 9.4 文件-文件的打开与关闭
- Linux C编程学习笔记(2):open、creat、close函数及文件的创建、打开与关闭
- C语言 文件的概念与操作(打开、读写和关闭等)
- C语言 文件的概念与操作(打开、读写和关闭等)
- 装饰设计模式
- rem适配所有屏幕大小demo分享
- 一星期英语音标特训
- 设置springboot为热部署
- J-Final框架学习
- 文件的创建与删除,打开与关闭(C)
- 快速排序
- 神奇的Python装饰器
- LeetCode-Trapping Rain Water
- python yield from的使用
- Linux启动过程详解
- 学生管理系统——查漏补缺
- html5的表单
- 栈和队列---求最大子矩阵的大小