文件操作
来源:互联网 发布:湖北楚天网络 编辑:程序博客网 时间:2024/06/08 14:12
文件操作代码框架
*/
#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;
}
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- 文件操作
- Swift - 序
- 结构型模式-外观模式
- Mongodb千万级数据在python下的综合压力测试及应用探讨
- SUSE助力SAP HANA实现高可用性
- Linux上强大的编译器gcc
- 文件操作
- 多校 hdu 5294 Tricks Device 最短路+最大流
- 三款开源关系型数据库对比:MySQL、PostgreSQL、SQLite
- Git stash和stash pop命令
- Leetcode Q2: Add Two Numbers
- Java中String类的方法及说明
- C#中DataTable、Dataset转Json和Json转DataTable
- Linux小白成长记--linux入门准备
- USACO1.3.3 Calf Flac