5月14日

 读取文件

​
#include<stdio.h>
#include<inttypes.h>
#define SIZE 50
int main() {
	
	FILE* file_stream = NULL;
	char buffer[256];
	//fopen fopen_s
	//打开文件,设定路径,设定文件的操作模式r
	errno_t err = fopen_s(&file_stream, "D:\\桌面\\demo1.txt", "r");
	if (err == 0 || file_stream == NULL) {
		printf("%d\n", err);
	}
	while (fgets(buffer, sizeof(buffer), file_stream) != NULL) {
		printf("%s\n", buffer);
	}
	fclose(file_stream);
	return 0;
}

​

 有时会出现读取文件输出乱码的情况,是因为文本文档编码格式的原因,可以将默认的UTF-8格式改成ANSI格式就可以正常读取文本了

​
#include<stdio.h>
#include<inttypes.h>
#include<stdlib.h>
#define SIZE 50
int main() {
	//静态数组大小,生命周期,作用域在编译的时候就确定了
	int static_arr[5] = { 1,2,3,4,5 };//栈
	//
	int* dynamic_arr = malloc(5 * sizeof(int));//堆
	if (dynamic_arr == NULL) {
		perror("动态数组分配失败");
		exit(EXIT_FAILURE);
	}
	for (size_t i = 0; i < 5; i++) {
		dynamic_arr[i] = (i + 1) * 10;
	}
	puts("动态数组的内容是:");
	for (size_t i = 0; i < 5; i++) {
		printf("%d ", dynamic_arr[i]);
	}
	//void*指针在C语言中用于泛型编程
	//堆内存要手动释放
	free(dynamic_arr);
	return 0;
}

​

 realloc()函数

#include<stdio.h>
#include<inttypes.h>
#include<stdlib.h>
#define SIZE 50
//realloc()函数
void printfBudgets(double* budgets, int size);
int main() {
	//动态调整部门预算列表
	int size = 3;//初始的预算项数
	double* budgets = (double*)malloc(size * sizeof(double));
	if (budgets == NULL) {
		perror("Fail to allocate initail budgets");
		return EXIT_FAILURE;
	}
	budgets[0] = 1000;
	budgets[1] = 1500;
	budgets[2] = 2000;
	printfBudgets(budgets, size);
	//需要增加预算项
	int newSize = 5;
	double* newBudgets = (double*)realloc(budgets, newSize * sizeof(double));
	if (newBudgets == NULL) {
		perror("Fail to allocate initail budgets");
		free(budgets);
		return EXIT_FAILURE;
	}
	budgets = newBudgets;
	budgets[3] = 2500;
	budgets[4] = 3000;
	printfBudgets(budgets, newSize);
	free(budgets);
	return EXIT_SUCCESS;
}
void printfBudgets(double* budgets, int size) {
	printf("Current budgets:\n");
	for (size_t i = 0; i < size; i++) {
		printf("Department %d: $%.2f\n", i + 1, budgets[i]);
	}
}

动态数组案例

#include<stdio.h>
#include<inttypes.h>
#include<stdlib.h>
#define SIZE 50
typedef struct Character {
	char* name;
	int level;
	int hp;
}Character;
Character* create_character(const char* name, int level, int hp) {
	Character* new_character = (Character*)malloc(sizeof(Character));
	if (new_character == NULL) {
		perror("Fail");
		return NULL;
	}
	new_character->name = (char*)malloc(strlen(name) + 1);
	if (new_character->name == NULL) {
		perror("Fail");
		free(new_character);
		return NULL;
	}
	strcpy_s(new_character->name, strlen(name) + 1, name);
	new_character->level = level;
	new_character->hp = hp;
	return new_character;
}
void free_character(Character* character) {
	if (character != NULL) {
		free(character->name);//释放名字占用空间
		free(character);
	}
}
int main() {
	Character* hero = create_character("Hero", 1, 100);
	if (hero == NULL) {
		return EXIT_FAILURE;
	}
	free_character(hero);
	return EXIT_SUCCESS;
}

#include<stdio.h>
#include<inttypes.h>
#include<stdlib.h>
//函数指针(Fuction pointer)
//指针指向函数
//回调函数

//返回类型(*指针变量名)(参数类型)
int (*myFunctionPointer)(int, int);
//->int f(int num1,int num2)
int add(int a, int b);
int main() {
	//指向函数,通过指针调用函数
	myFunctionPointer = add;
	int result = myFunctionPointer(2, 3);
	printf("result:%d", result);
	//实现回调函数
	return 0;
}
int add(int a, int b) {
	return a + b;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值