C语言数据管理技术

1 链表数据结构

        链表是C语言中一种与数组不同的存储结构,通过指针将内存中的各结点联系起来,能够更灵活地处理数据。

1.1 设计代码

#include 
#include 
#include 
struct stu
{
	int num; //学号
	int age; //年龄
	struct stu* next;
};
struct stu* creat(int n)
{
	struct stu* head = nullptr, * pf = nullptr, * pb;
	int i, ret;
	for (i = 0; i < n; i++)
	{
		pb = (struct stu*)malloc(sizeof(struct stu));
		if (pb != nullptr)
		{
			memset(pb, 0, sizeof(struct stu));
			printf("input Number and Age\n");
			ret = scanf("%d%d", &pb->num, &pb->age);
			pb->next = nullptr;
		}
		if (pf == nullptr)
		{
			pf = head = pb;
		}
		else
		{
			pf->next = pb;
			pf = pb;
		}
	}
	return head;
}
void print(struct stu* head)
{
	while (head) {
		printf("%d %d\n", head->num, head->age);
		head = head->next;
	}
}
/*
1 19
2 21
*/
int main()
{
	struct stu* head = creat(2);
	print(head);
	return 0;
}

1.2 执行结果

C语言数据管理技术_第1张图片 图1 链表数据结构代码执行结果

2 打开文件并进行判断和关闭文件

        文本文件的打开与关闭,是文本文件读写的前提与基础。

2.1 设计代码

#include 

int main()
{
	FILE* fp;
	fp = fopen("test.txt", "r");
	if (fp == NULL) {
		printf("fail to open the file!\n");
	}
	else {
		printf("The file is open!\n");
		fclose(fp);
	}
	return 0;
}

2.2 执行结果

图2 打开文件并进行判断和关闭文件代码执行结果

3 fgetc()函数的使用

        fgetc()函数,与while循环结合,能够将文本文件中的各字符依次读出。

3.1 设计代码

#include 

int main()
{
	FILE *fp;
	fp = fopen("test.txt", "r");
	if (fp != NULL)
	{
		while (!feof(fp))
		{
			printf("%c", fgetc(fp));
		}
		fclose(fp);
	}
	else
	{
		printf("fail to open!\n");
	}
	return 0;
}

3.2 执行结果

图3 fgetc()函数的使用代码执行结果

4 fputc()函数的使用

        fputc()函数,与while循环相结合,能够将一行字符串存储到文本文件中。

4.1 设计代码

#include 

int main()
{
	char filename[20] = {0}, ch;
	int ret;
	FILE *fp;
	printf("Enter a filename: ");
	ret = scanf("%s", filename);
	ch = getchar();//读出换行符
	printf("Enter some characters to output to file: ");
	if ((fp = fopen(filename, "w")) == NULL)
	{
		printf("fail to open!\n");
	}
	else {
		while ((ch = getchar()) != '\n') {
			fputc(ch, fp);
		}
		fclose(fp);
	}
	return 0;
}

4.2 执行结果

图4 fputc()函数的使用代码执行结果

5 fprintf和fscanf函数的使用

        fprintf,能够格式化输出数据到文件中;fscanf,能够格式化读取文件中的数据。

5.1 设计代码

#include 
int main()
{
	FILE *fp;
	int num = 10, ret;
	char name[10] = "Leeming";
	char gender = 'M';
	if ((fp = fopen("info.txt", "w+")) == NULL)
	{
		printf("can't open the file!\n");
	}
	else
	{
		//将数据格式化输出到文件info.txt中
		fprintf(fp, "%d,%s,%c", num, name, gender);
	}
	if (fp != NULL) {
		//从文件info.txt中格式化读取数据
		ret = fscanf(fp, "%d,%s,%c", &num, name, &gender);
		//格式化输出到屏幕
		printf("%d,%s,%c\n", num, name, gender);
		fclose(fp);
	}
	return 0;
}

5.2 执行结果

图5 fprintf和fscanf函数的使用代码执行结果

6 fread和fwrite函数的使用

        fread与fwrite函数,能够读写二进制文件。

6.1 设计代码

#include 
#define SIZE 3
typedef enum{MM, GG} Gender;
typedef struct
{
	char name[10];
	int age;
	Gender gender;
}Person;
void write2file(Person emp[SIZE])
{
	FILE *fp;
	if ((fp = fopen("emp.txt", "wb")) == NULL)
	{
		printf("cannot open file!\n");
		return;
	}
	for (int i = 0; i < SIZE; i++)
	{
		if (fwrite(&emp[i], sizeof(Person), 1, fp) != 1)
		{
			printf("file write error!\n");
		}
	}
	fclose(fp);
}
void read_from_file(FILE *fp)
{
	Person emp_out[SIZE];
	if ((fp = fopen("emp.txt", "rb")) == NULL)
	{
		printf("cannot open file!\n");
		return;
	}
	printf("\n%d employee's information read: \n", SIZE);
	for (int i = 0; i < SIZE; i++)
	{
		if (fread(&emp_out[i], sizeof(Person), 1, fp) != 1)
		{
			if (feof(fp))
			{
				fclose(fp);
				return;
			}
		}
		printf("%-5s %4d %5d\n", emp_out[i].name, emp_out[i].age, emp_out[i].gender);
	}
	fclose(fp);
}
int main()
{
	FILE *fp = NULL;
	Person employee[SIZE];
	int ret;
	printf("Enter %d employee's information:\n", SIZE);
	for (int i = 0; i < SIZE; i++)
	{
		ret = scanf("%s %d %d", employee[i].name, &employee[i].age, &employee[i].gender);
	}
	write2file(employee);
	read_from_file(fp);
	return 0;
}

6.2 执行结果

C语言数据管理技术_第2张图片 图6 fread和fwrite函数的使用代码执行结果

7 编程心得

  • 链表数据结构可以非常灵活地存储数据。
  • 文本文件的读写在编程中有着非常重要的作用。
  • 二进制文件在程序设计中有着广泛的使用。

你可能感兴趣的:(C/C++课程设计,c语言,开发语言,链表,算法)