读取文件
#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;
}