????????链表是C语言中一种与数组不同的存储结构,通过指针将内存中的各结点联系起来,能够更灵活地处理数据。
#include <cstdio>
#include <cstdlib>
#include <cstring>
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;
}
????????文本文件的打开与关闭,是文本文件读写的前提与基础。
#include <cstdio>
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;
}
????????fgetc()函数,与while循环结合,能够将文本文件中的各字符依次读出。
#include <cstdio>
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;
}
????????fputc()函数,与while循环相结合,能够将一行字符串存储到文本文件中。
#include <cstdio>
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;
}
????????fprintf,能够格式化输出数据到文件中;fscanf,能够格式化读取文件中的数据。
#include <cstdio>
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;
}
????????fread与fwrite函数,能够读写二进制文件。
#include <cstdio>
#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;
}