在這裡我們創建一個結構體數組,該數組有三名老師,每名老師又帶有三名同學,我們分别給他們賦值,最後根據老師的名字進行降序排列。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
//結構體類型,每個導師有3個學生
typedef struct Teacher
{
char* tName;//導師
char** stu;//
int age;
}Teacher;
//在createTeacher中分配空間
int createTeacher(Teacher** p, int n1, int n2)
{
if (p==NULL)
{
return -1;
}
Teacher* temp = NULL;//Teacher temp[3]
temp = (Teacher*)malloc(n1 * sizeof(Teacher));
if (temp==NULL)
{
return -2;
}
for (int i = 0; i < n1; i )
{
//導師
temp[i].tName = (char*)malloc(30);//3個老師
//學生
char** s = (char**)malloc(n2 * sizeof(char*));//3個學生的指針變量
for (int j = 0; j < n2; j )//給3個學生分配堆空間
{
s[j] = (char*)malloc(30);
}
//直接賦值
temp[i].stu = s;
}
//間接賦值
*p = temp;
return 0;
}
//給成員賦值
void initTeacher(Teacher* p, int n1, int n2)
{
if (p==NULL)
{
return;
}
char buf[30];
for (int i = 0; i < n1; i )
{
//導師名字
sprintf(buf, "teacher%d%d%d", i, i, i);
strcpy(p[i].tName, buf);
//年齡
p[i].age = 30 2 * i;
//學生
for (int j = 0; j < n2; j )
{
sprintf(buf,"stu%d%d%d%d", i, i, j, j);
strcpy(p[i].stu[j], buf);
}
}
}
//打印結構體成員信息
void printTeacher(Teacher* p, int n1, int n2)
{
if (p==NULL)
{
return;
}
for (int i = 0; i < n1; i )
{
//年齡
printf("%s[%d]\n", p[i].tName, p[i].age);
//學生
for (int j = 0; j < n2; j )
{
printf("\t%s", p[i].stu[j]);
}
printf("\n");
}
}
//根據導師名字排序,降序
void sortTeacher(Teacher* p, int n)
{
if (p==NULL)
{
return;
}
Teacher temp;
for (int i = 0; i < n-1; i )
{
for (int j = i 1; j < n; j )
{
if (strcmp(p[i].tName,p[j].tName)<0)
{
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
}
//釋放空間,在函數内部把p賦值為NULL
void freeTeacher(Teacher** p, int n1, int n2)
{
if (p==NULL)
{
return;
}
Teacher* temp = *p;
for (int i = 0; i < n1; i )
{
//釋放導師
if (temp[i].tName!=NULL)
{
free(temp[i].tName);
temp[i].tName = NULL;
}
//釋放學生
for (int j = 0; j < n2; j )
{
if (temp[i].stu[j]!=NULL)
{
free(temp[i].stu[j]);
temp[i].stu[j] = NULL;
}
}
if (temp[i].stu != NULL)
{
free(temp[i].stu);
temp[i].stu = NULL;
}
}
if (temp!=NULL)
{
free(temp);
*p = NULL;
}
}
int main()
{
int ret = 0;
int n1 = 3;//導師個數
int n2 = 3;//學生
Teacher* p = NULL;
ret = createTeacher(&p, n1, n2);
if (ret!=0)
{
printf("createTeacher err:%d\n", ret);
return ret;
}
initTeacher(p, n1, n2);//給成員賦值
//打印成員,排序前
printf("排序前:\n");
printTeacher(p, n1, n2);
//根據導師姓名排序,降序
sortTeacher(p, n1);
//打印成員,排序後
printf("排序後\n");
printTeacher(p, n1, n2);
//釋放空間,在函數内部把p賦值為null
freeTeacher(&p, n1, n2);
printf("\n");
return 0;
}
打印結果
按照字符讀寫文件fgetc、fputc的使用fgetc():
int fgetc(FILE *stream)
參數:
返回值
該函數以無符号 char 強制轉換為 int 的形式返回讀取的字符,如果到達文件末尾或發生讀錯誤,則返回 EOF。
fputc():
int fputc(int char, FILE *stream)
參數:
返回值:
如果沒有發生錯誤,則返回被寫入的字符。如果發生錯誤,則返回 EOF,并設置錯誤标識符。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int main0301()
{
fputc('a', stdout);//将字符a輸出到屏幕,打印普通信息
char ch;
ch = fgetc(stdin);//從鍵盤輸入字符放到ch中
printf("ch=%c", ch);
fprintf(stderr, "%c", ch);//stderr指向屏幕,打印錯誤信息
fputc(ch, stderr);
return 0;
}
int main0302()
{
FILE* fp = NULL;
//相對路徑, / , ./ , ../ linux與windows均可
//vs:編譯代碼時,相對于項目工程
//直接運行可執行程序,相對于程序
char* p = "aaaaaaaaaa"
"bbbbbbbbbbbb"; //字符串換行
printf("%s\n", p);
fp = fopen("./a.txt", "w ");
if (fp!=NULL)
{
fclose(fp);
fp == NULL;
}
return 0;
}
void my_fputc(char* path)
{
FILE* fp = NULL;
// w 表示可寫可讀方式打開 若文件不存在,則創建文件
// 若文件存在 則清空文件内容再寫
fp = fopen(path, "w ");
if (fp==NULL)
{
perror("fopen");
return;
}
//寫文件
char buf[] = "Today is saturday";
for (int i = 0; i < strlen(buf); i )
{
//返回值是成功寫入文件的字符
int ch = fputc(buf[i], fp);
printf("ch=%c\n", ch);
}
if (fp!=NULL)
{
fclose(fp);
fp = NULL;
}
}
void my_fgetc(char*path)
{
//以文件讀寫方式打開,如果文件不存在 打開失敗
FILE* fp = fopen(path, "r ");
if (fp==NULL)
{
perror("my_fgetc fopen");
return;
}
char ch;
while (ch=fgetc(fp)!=EOF)
{
printf("%c ", ch);
}
printf("\n");
while (!feof(fp))//文件沒有結束執行循環
{
ch = fgetc(fp);
printf("%c ", ch);
}
printf("\n");
if (fp!=NULL)
{
fclose(fp);
fp = NULL;
}
}
int main()
{
// my_fputc("../a.txt");
my_fgetc("../a.txt");
return 0;
}
fgets():
char *fgets(char *str, int n, FILE *stream)
fputs():
int fputs(const char *str, FILE *stream)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
void my_fputs(char* path)
{
FILE* fp = NULL;
// w 表示可寫可讀方式打開 若文件不存在,則創建文件
// 若文件存在 則清空文件内容再寫
fp = fopen(path, "w ");
if (fp == NULL)
{
perror("fopen");
return;
}
char* buf[] = { "aaa\n","bbb\n","ccc\n" };
for (int i = 0; i < strlen(buf); i )
{
//返回值是成功寫入文件個數,成功返回0,失敗非0
int len = fputs(buf[i], fp);
printf("len=%d\n", len);
}
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
}
void my_fgets(char* path)
{
//以文件讀寫方式打開,如果文件不存在 打開失敗
FILE* fp = fopen(path, "r ");
if (fp == NULL)
{
perror("my_fgets fopen");
return;
}
char buf[100] = { 0 };
while (!feof(fp))
{
//fgets 的返回值,成功返回讀取文件内容
//fgets讀取完畢後,字符串末尾自動加0
//會将\n讀取,并且以\n作為換行标志
char* p = fgets(buf, sizeof(buf), fp);
printf("buf=%s\n", buf);
printf("%s ",p);
}
printf("\n");
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
}
int main()
{
my_fputs("../04.txt");
my_fgets("../04.txt");
return 0;
}
打印結果
按照塊讀寫文件fread、fwritefread():
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
成功讀取的元素總數會以 size_t 對象返回,size_t 對象是一個整型數據類型。如果總數與 nmemb 參數不同,則可能發生了一個錯誤或者到達了文件末尾
fwrite():
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
如果成功,該函數返回一個 size_t 對象,表示元素的總數,該對象是一個整型數據類型。如果該數字與 nmemb 參數不同,則會顯示一個錯誤。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
typedef struct Stu
{
char name[50];
int id;
}Stu;
void my_fwrite(char*path)
{
FILE* fp = fopen(path, "w ");
if (fp == NULL)
{
perror("my_write fopen");
return;
}
Stu s[3];
char buf[50];
for (int i = 0; i < 3; i )
{
sprintf(buf, "stu%d%d%d", i, i, i);
strcpy(s[i].name, buf);
s[i].id = i 1;
}
//按塊寫文件
//s 寫入文件内容的首地址
//sizeof(Stu) 塊數據的大小
//3 塊數 寫文件的數據大小 sizeof(Stu)*3
//fp 文件指針
//返回值 成功寫入文件的塊數
int ret = fwrite(s, sizeof(Stu), 3, fp);
printf("ret=%d\n", ret);//3
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
}
void my_fread(char** path)
{
FILE* fp = fopen(path, "r ");
if (fp == NULL)
{
perror("my_fread fopen");
return;
}
Stu s[3];
char buf[50];
for (int i = 0; i < 3; i )
{
sprintf(buf, "stu%d%d%d", i, i, i);
strcpy(s[i].name, buf);
s[i].id = i 1;
}
//按塊讀文件
//s 放文件内容首地址
//sizeof(Stu) 塊數據的大小
//3 塊數 讀文件的數據大小 sizeof(Stu)*3
//fp 文件指針
//返回值 成功讀出文件的塊數
int ret = fread(s, sizeof(Stu), 3, fp);
printf("ret=%d\n", ret);//3
for (int i = 0; i < 3; i )
{
printf("%s,%d\n", s[i].name, s[i].id);
}
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
}
int main()
{
my_fwrite("../05.txt");
my_fread("../05.txt");
return 0;
}
打印結果
按照格式化讀寫文件fprintf、fscanffprintf():
int fprintf(FILE *stream, const char *format, ...)
fscanf():
int fscanf(FILE *stream, const char *format, ...)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
void my_fprintf(char**path)
{
FILE* fp = fopen(path, "w ");
if (fp == NULL)
{
perror("my_fprintf fopen");
return;
}
//printf("hello world%d\n", 001);
//fprintf(stdout, "hello world%d\n", 001);//實際結果與上相同
fprintf(fp, "%d %d %d\n", 001,2,3);
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
}
void my_fscanf(char** path)
{
FILE* fp = fopen(path, "r ");
if (fp == NULL)
{
perror("my_fscanf fopen");
return;
}
//printf("hello world%d\n", 001);
//fprintf(stdout, "hello world%d\n", 001);//實際結果與上相同
int a = 0;
int b = 0;
int c = 0;
fscanf(fp, "%d %d %d\n", &a,&b,&c);
printf("a=%d,b=%d,c=%d\n", a,b,c);
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
}
int main()
{
my_fprintf("../06.txt");
my_fscanf("../06.txt");
return 0;
}
fseek():
int fseek(FILE *stream, long int offset, int whence)
SEEK_SET |
文件的開頭 |
SEEK_CUR |
文件指針的當前位置 |
SEEK_END |
文件的末尾 |
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
typedef struct Stu
{
char name[50];
int id;
}Stu;
void my_fwrite(char* path)
{
FILE* fp = fopen(path, "w ");
if (fp == NULL)
{
perror("my_write fopen");
return;
}
Stu s[3];
char buf[50];
for (int i = 0; i < 3; i )
{
sprintf(buf, "stu%d%d%d", i, i, i);
strcpy(s[i].name, buf);
s[i].id = i 1;
}
//按塊寫文件
//s 寫入文件内容的首地址
//sizeof(Stu) 塊數據的大小
//3 塊數 寫文件的數據大小 sizeof(Stu)*3
//fp 文件指針
//返回值 成功寫入文件的塊數
int ret = fwrite(s, sizeof(Stu), 3, fp);
printf("ret=%d\n", ret);//3
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
}
void my_fread(char** path)
{
FILE* fp = fopen(path, "r ");
if (fp == NULL)
{
perror("my_fread fopen");
return;
}
Stu s[3];
Stu temp;//讀三個結構體
fseek(fp, 2*sizeof(Stu), SEEK_SET);
//fseek(fp, -1*sizeof(Stu), SEEK_END);
int ret = fread(&temp, sizeof(Stu), 1, fp);
if (ret==1)
{
printf("[temp]=%s,%d\n", temp.name, temp.id);
}
//将文件光标移動到開始位置
//fseek(fp, 0, SEEK_SET);
rewind(fp);//光标置首
//按塊讀文件
//s 放文件内容首地址
//sizeof(Stu) 塊數據的大小
//3 塊數 讀文件的數據大小 sizeof(Stu)*3
//fp 文件指針
//返回值 成功讀出文件的塊數
ret = fread(s, sizeof(Stu), 3, fp);
printf("ret=%d\n", ret);//3
for (int i = 0; i < 3; i )
{
printf("%s,%d\n", s[i].name, s[i].id);
}
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
}
int main()
{
my_fwrite("../07.txt");
my_fread("../07.txt");
return 0;
}
打印結果
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!