文件操作

/*
   文件操作代码框架
   */
#include <stdio.h>
int main() {
FILE *p_file = fopen("a.txt", "w");
/*if (p_file) {
//
fclose(p_file);
p_file = NULL;
}*/
if (!p_file) {
return 0;
}
//
fclose(p_file);
p_file = NULL;
return 0;

}

/*
   touch命令练习
   */
#include <stdio.h>
int main(int argc, char *argv[]) {
FILE *p_file = fopen(argv[1], "wb");
if (p_file) {
fclose(p_file);
p_file = NULL;
}
return 0;
}


/*
   fwrite函数演示
   */
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5}, size = 0;
FILE *p_file = fopen("a.bin", "wb");
if (p_file) {
size = fwrite(arr, sizeof(int), 5, p_file);
printf("一共写入%d个整数类型存储区\n", size);
fclose(p_file);
p_file = NULL;
}
return 0;
}


/*
   fread函数演示
   */
#include <stdio.h>
int main() {
int arr[5] = {}, size = 0, num = 0;
FILE *p_file = fopen("a.bin", "rb");
if (p_file) {
size = fread(arr, sizeof(int), 10, p_file);
printf("一共获得%d个存储区\n", size);
for (num = 0;num <= 4;num++) {
printf("%d ", arr[num]);
}
printf("\n");
fclose(p_file);
p_file = NULL;
}
return 0;
}



/*
   文件练习
   */
#include <stdio.h>
int main() {
int arr[][5] = {1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25};
int num = 0, row = 0, col = 0;
FILE *p_file = fopen("a.bin", "wb");
//写文件内容
if (p_file) {
        for (num = 0;num <= 4;num++) {
fwrite(arr[num], sizeof(int), 5, p_file);
}
fclose(p_file);
p_file = NULL;
}
//读文件内容
p_file = fopen("a.bin", "rb");
if (p_file) {
for (num = 4;num >= 0;num--) {
fread(arr[num], sizeof(int), 5, p_file);
}
fclose(p_file);
p_file = NULL;
}
//打印文件内容
for (row = 0;row <= 4;row++) {
for (col = 0;col <= 4;col++) {
printf("%2d ", arr[row][col]);
}
printf("\n");
}
return 0;
}






#include<stdio.h>
int main(int argc,char *argv[]){
int size;
char arr[100]={};
FILE *p_num1=NULL,*p_num2=NULL;
p_num1=fopen(argv[1],"rb");
if(!p_num1){
return 0;
}
p_num2=fopen(argv[2],"wb");
if(!p_num2){
fclose(p_num2);
p_num2=NULL;
return 0;
}
while(size=fread(arr,sizeof(char),100,p_num1)){
size=fread(arr,sizeof(char),100,p_num1);
fwrite(arr,sizeof(char),size,p_num2);
}
fclose(p_num1);
p_num1=NULL;
return 0;
}



/*
   文件拷贝作业
   */
#include <stdio.h>
int main(int argc, char *argv[]) {
char buf[100] = {};
FILE *p_src = NULL, *p_dest = NULL;
int size = 0;
    p_src = fopen(argv[1], "rb");
if (!p_src) {
return 0;
}
p_dest = fopen(argv[2], "wb");
if (!p_dest) {
fclose(p_src);
p_src = NULL;
return 0;
}
while (size = fread(buf, sizeof(char), 100, p_src)) {
fwrite(buf, sizeof(char), size, p_dest);
}
fclose(p_dest);
p_dest = NULL;
fclose(p_src);
p_src = NULL;
return 0;
}



/*
   人员信息管理系统练习
   */
#include <stdio.h>
#include <string.h>
int main() {
int id = 0, choice = 1;
float salary = 0.0f;
char name[20] = {};
FILE *p_file = fopen("person.bin", "ab");
if (p_file) {
do {
printf("请输入人员id:");
scanf("%d", &id);
printf("请输入工资:");
scanf("%g", &salary);
scanf("%*[^\n]");
scanf("%*c");
printf("请输入姓名:");
fgets(name, 20, stdin);
            if (strlen(name) == 19 && name[18] != '\n') {
scanf("%*[^\n]");
scanf("%*c");
}
fwrite(&id, sizeof(int), 1, p_file);
fwrite(&salary, sizeof(float), 1, p_file);
fwrite(name, sizeof(char), 20, p_file);
printf("是否需要输入下一个人员信息?0表示不需要,1表示需要");
scanf("%d", &choice);
} while (choice);
fclose(p_file);
p_file = NULL;
}
return 0;
}



/*
   察看人员信息练习
   */
#include <stdio.h>
int main() {
int id = 0;
float salary = 0.0f;
char name[20] = {};
FILE *p_file = fopen("person.bin", "rb");
if (p_file) {
        while (fread(&id, sizeof(int), 1, p_file)) {
fread(&salary, sizeof(float), 1, p_file);
fread(name, sizeof(char), 20, p_file);
printf("id是%d\n", id);
printf("工资是%g\n", salary);
printf("姓名是%s\n", name);
}
fclose(p_file);
p_file = NULL;
}
return 0;
}



/*
   察看人员信息练习
   */
#include <stdio.h>
int main() {
int id = 0, oldid = 0;
float salary = 0.0f;
char name[20] = {};
printf("请输入要查找的id:");
scanf("%d", &oldid);
FILE *p_file = fopen("person.bin", "rb");
if (p_file) {
        while (fread(&id, sizeof(int), 1, p_file)) {
fread(&salary, sizeof(float), 1, p_file);
fread(name, sizeof(char), 20, p_file);
if (id == oldid) {
   printf("id是%d\n", id);
   printf("工资是%g\n", salary);
   printf("姓名是%s\n", name);
}
}
fclose(p_file);
p_file = NULL;
}
return 0;
}



/*
   位置指针演示
   */
#include <stdio.h>
int main() {
char ch = 0;
FILE *p_file = fopen("a.txt", "rb");
if (p_file) {
rewind(p_file);
printf("位置指针数值是%ld\n", ftell(p_file));
fread(&ch, sizeof(char), 1, p_file);
printf("%c\n", ch);
rewind(p_file);
printf("位置指针数值是%ld\n", ftell(p_file));
fread(&ch, sizeof(char), 1, p_file);
printf("%c\n", ch);
rewind(p_file);
printf("位置指针数值是%ld\n", ftell(p_file));
fread(&ch, sizeof(char), 1, p_file);
printf("%c\n", ch);
fclose(p_file);
p_file = NULL;
}
return 0;
}



/*
   文件位置指针演示
   */
#include <stdio.h>
int main() {
char ch = 0;
FILE *p_file = fopen("a.txt", "rb");
if (p_file) {
fseek(p_file, 2, SEEK_SET);
fread(&ch, sizeof(char), 1, p_file);
printf("%c\n", ch);
fseek(p_file, 2, SEEK_CUR);
fread(&ch, sizeof(char), 1, p_file);
printf("%c\n", ch);
        fseek(p_file, -3, SEEK_END);
fread(&ch, sizeof(char), 1, p_file);
printf("%c\n", ch);
fclose(p_file);
p_file = NULL;
}
return 0;
}



/*
   察看人员信息练习
   */
#include <stdio.h>
int main() {
int id = 0, oldid = 0;
float salary = 0.0f;
char name[20] = {};
printf("请输入要查找的id:");
scanf("%d", &oldid);
FILE *p_file = fopen("person.bin", "rb");
if (p_file) {
        while (fread(&id, sizeof(int), 1, p_file)) {
if (id == oldid) {
           fread(&salary, sizeof(float), 1, p_file);
           fread(name, sizeof(char), 20, p_file);
   printf("id是%d\n", id);
   printf("工资是%g\n", salary);
   printf("姓名是%s\n", name);
}
else {
fseek(p_file, 24, SEEK_CUR);
}
}
fclose(p_file);
p_file = NULL;
}
return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值