结构体+指针+文件读写操作

一.1.声明了该结构体就声明了结构体内所有成员。

#include <stdio.h>

typedef struct stuInfo
{
    char *name;
    int  age;
    int  num;
}Student;

int main(int argc, const char * argv[])
{

    // insert code here...
    printf("Hello, World!
");
    Student s = {"Jobs",18,1};
//    printf("请输入名字/年龄/学号
");……Student s[3];//存有3个结构体的数组
//    for (int i = 0; i < 3; i++)
//    {
//        scanf("%s%d%d",s[i].name,&s[i].age,&s[i].num);
//    }
    printf("%s--------%d----------%d
",s.name,s.age,s.num);
    //指向某结构体的结构体指针。非指针的话,结构体变量点成员进行读写
    Student *p;
    p = &s;
    p->age = 10;//等同(*p).age  右结合,->表示更清晰,意思就是指向一个结构体变量的指针
    p->name = "bill";
    p->num = 1;
    
    printf("%s--------%d----------%d
",p->name,p->age,p->num);
    return 0;
}

需求:1.创建2.输出3.查找4.增加5.删除

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
struct Stuinfo
{
    char ID[4];
    char NAME[10];
    int Score;
};

struct Stuinfo stuInfo[100];//结构体数组

FILE *file;//文件指针,指向物理文件

bool checkId(char Id[]);
bool checkId(char Id[])
{
    bool s = false;
    for (int i = 0; i < strlen(Id); i++)
    {
        if (Id[i] >= '0' && Id[i] <= '9')
        {
            s = true;
        }
        else
        {
            s = false;
            break;   //
        }
    }
    return s;
}

bool checkName(char name[]);
bool checkName(char name[])
{
    bool s = false;
    for (int i = 0; i < strlen(name); i++)
    {
        if ((name[i] >= 'a' && name[i] <= 'z') || (name[i] >= 'A' && name[i] <= 'Z'))
        {
            s = true;
        }
        else
        {
            s = false;
            break;   //
        }
    }
    return s;
}

void createDB();
void createDB()
{
    int i = 0;//代表先在录入第i个学生
    int m = 0;//录入了多少学生
    bool success = 0;
    
    while (1)
    {
        //学号
        printf("请输入第%d个学生的信息
",i+1);
        do
        {
            printf("请输入学号
");
            char tempId[4];
            scanf("%s",tempId);
            
            if (checkId(tempId))
            {
                success = 1;
                strcpy(stuInfo[i].ID, tempId);
            }
            else
            {
                success = 0;
                printf("输入的学号有错误
");
            }
        } while (!success);
        //姓名
        do
        {
            printf("请输入姓名
");
            char tempName[10];
            scanf("%s",tempName);
            
            if (checkName(tempName))
            {
                success = 1;
                strcpy(stuInfo[i].NAME, tempName);
            }
            else
            {
                success = 0;
            }
        } while (!success);
        
        //成绩
        do
        {
            printf("请输入成绩
");
            int score = 0;
            scanf("%d",&score);
            
            if (score >= 0 && score <=100)
            {
                success = 1;
                stuInfo[i].Score = score;
            }
            else
            {
                success = 0;
            }
        } while (!success);
        
        i++;
        m++;
        
        //跳出循环的条件
        printf("是继续录入信息请按y或Y
");
        getchar();//截取

        char ch;
        scanf("%c",&ch);
        if (ch == 'Y' || ch == 'y')
        {
            continue;
        }
        else
        {
            break;
        }
    }
    
    //把数据记录到文件
    if ((file = fopen("/Users/3022/Desktop/code_C/评讲/stuInfo/stu.txt","w+")) == NULL)
    {
        printf("打开文件失败
");
    }
    for (int i = 0 ; i < m; i++)
    {
        fwrite(&stuInfo[i], sizeof(struct Stuinfo), 1, file);//只是写到缓冲区
    }
    
    fclose(file);//写到文件上
}

void printStuInfo();
void printStuInfo()
{
    if ((file = fopen("/Users/3022/Desktop/code_C/评讲/stuInfo/stu.txt","r")) == NULL)
    {
        printf("打开文件失败
");
    }

    int n = 0;
                            //代表文件中几条记录
    for(int i = 0;fread(&stuInfo[i],sizeof(struct Stuinfo), 1, file);i++ )
    {
        n++;
    }

    printf("学号--------姓名-------成绩-----
");
    for(int j = 0;j < n ;j++)
    {
        printf("%-10s %-10s %-10d
",stuInfo[j].ID,stuInfo[j].NAME,stuInfo[j].Score);
    }
    printf("------------------------------
");
    fclose(file);
}
int main(int argc, const char * argv[])
{
    //createDB();
    printStuInfo();
    
    return 0;
}

 2ex

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100


struct stuInfo
{
    char Id[4];
    char Name[10];
    int Score;
};

typedef struct stuInfo StudentInfo;

StudentInfo studentInfo[N];

FILE *file;

void createDataBase2(int m);
void createDataBase2(int m)
{
    int flag = 0;
    int len = m;
    
    while (1)
    {
        printf("请录入学生信息:
");
        
        //创建学号
        do
        {
            printf("请输入学号:
");
            
            char tmpId[4];
            scanf("%s",tmpId);
            if (strlen(tmpId) > 4)
            {
                flag = 1;
                printf("学号输入错误,请重新输入。。。
");
            }
            else
            {
                flag = 0;
                strcpy(studentInfo[m].Id, tmpId);
            }
            
        } while (flag);
        
        // 创建姓名
        do
        {
            printf("请输入姓名。。。
");
            char tmpName[10];
            scanf("%s",tmpName);
            if ((strlen(studentInfo[m].Name) <= 0) && (strlen(studentInfo[m].Name) > 10))
            {
                flag = 1;
                printf("请输入姓名。。。
");
            }
            else
            {
                flag = 0;
                strcpy(studentInfo[m].Name, tmpName);
            }
        } while (flag);
        
        
        // 录入成绩
        do
        {
            printf("请输入成绩。。。
");
            scanf("%d",&studentInfo[m].Score);
            if ((studentInfo[m].Score > 100)&&(studentInfo[m].Score < 0 ))
            {
                flag = 1;
                printf("请重新输入成绩。。。
");
            }
            else
            {
                flag = 0;
            }
        } while (flag);
        
        len++;
        printf("是否继续录入,请按y或Y继续
");
        char ch;
        getchar();//截取回车
        scanf("%c",&ch);
        
        //printf("%c
",ch);
        
        if((ch == 'y') || (ch == 'Y'))
        {
            continue;
        }
        else
        {
            break;
        }
    }
    
    
    // 打开文件
    if ((file = fopen("/Users/3022/Desktop/code_C/课后作业/h_09学生信息/stu.txt","a")) == NULL)
    {
        printf("打开文件失败");
    }
    
    for (int i = m; i < len; i++)
    {
        if (fwrite(&studentInfo[i], sizeof(StudentInfo), 1, file) != 1)
        {
            printf("写文件失败");
        }
    }
    fclose(file);
}

void createDataBase();
void createDataBase()
{
    int flag = 0;
    int i = 0;      //第几个学生
    int m = 0;      //已录入多少学生
    
    while (1)
    {
        printf("请录入第%d个学生信息:
",i+1);
        
        //创建学号
        do
        {
            printf("请输入学号:
");
            
            char tmpId[4];
            scanf("%s",tmpId);
            if (strlen(tmpId) > 4)
            {
                flag = 1;
                printf("学号输入错误,请重新输入。。。
");
            }
            else
            {
                flag = 0;
                strcpy(studentInfo[i].Id, tmpId);
            }
            
        } while (flag);
        
        // 创建姓名
        do
        {
            printf("请输入姓名。。。
");
            char tmpName[10];
            scanf("%s",tmpName);
            if ((strlen(studentInfo[i].Name) <= 0) && (strlen(studentInfo[i].Name) > 10))
            {
                flag = 1;
                printf("请输入姓名。。。
");
            }
            else
            {
                flag = 0;
                strcpy(studentInfo[i].Name, tmpName);
            }
        } while (flag);
        
        
        // 录入成绩
        do
        {
            printf("请输入成绩。。。
");
            scanf("%d",&studentInfo[i].Score);
            if ((studentInfo[i].Score > 100)&&(studentInfo[i].Score < 0 ))
            {
                flag = 1;
                printf("请重新输入成绩。。。
");
            }
            else
            {
                flag = 0;
            }
        } while (flag);
        
        i++;
        m++;
        
        
        printf("是否继续录入,请按y或Y继续
");
        char ch;
        getchar();//截取回车
        scanf("%c",&ch);
        
        //printf("%c
",ch);
        
        if((ch == 'y') || (ch == 'Y'))
        {
            continue;
        }
        else
        {
            break;
        }
    }
    
    
    // 打开文件
    if ((file = fopen("/Users/3022/Desktop/code_C/课后作业/h_09学生信息/stu.txt","w+")) == NULL)
    {
        printf("打开文件失败");
    }
    
    for (int i = 0; i < m; i++)
    {
        if (fwrite(&studentInfo[i], sizeof(StudentInfo), 1, file) != 1)
        {
            printf("写文件失败");
        }
    }
    fclose(file);
    
}

void printStuInfo();
void printStuInfo()
{
    printf("
所有的信息:
");
    
    if ((file = fopen("/Users/3022/Desktop/code_C/课后作业/h_09学生信息/stu.txt","rb+")) == NULL)
    {
        printf("打开文件失败");
    }
    
    int m = 0;
    for (int i = 0; fread(&studentInfo[i], sizeof(StudentInfo), 1, file); i++)
    {
        m++;
    }
    
    for (int i = 0; i < m; i++)
    {
        if (fread(&studentInfo[i], sizeof(StudentInfo), 1, file) == 0)
        {
            printf("读文件失败
");
        }
    }
    
    printf("学号======姓名======成绩===
");
    for (int i = 0 ; i < m; i++)
    {
        printf("%-8s %-8s %-8d
",studentInfo[i].Id,studentInfo[i].Name,studentInfo[i].Score);
    }
    printf("=========================
");
    fclose(file);
}


//按名字查询
void checkStuInfo();
void checkStuInfo()
{
    printf("
按名字查询为:
");
    char name[10];
    
    scanf("%s",name);
    
    //
    if ((file = fopen("/Users/3022/Desktop/code_C/课后作业/h_09学生信息/stu.txt","rb+")) == NULL)
    {
        printf("打开文件失败");
    }
    
    int m = 0;
    for (int i = 0; fread(&studentInfo[i], sizeof(StudentInfo), 1, file); i++)
    {
        m++;
    }
    
    for (int i = 0; i < m; i++)
    {
        if (fread(&studentInfo[i], sizeof(StudentInfo), 1, file) == 0)
        {
            printf("读文件失败
");
        }
    }
    
    printf("学号======姓名======成绩===
");
    for (int i = 0; i < m; i++)
    {
        if (strcmp(name,studentInfo[i].Name) == 0)
        {
            printf("%-8s %-8s %-8d
",studentInfo[i].Id,studentInfo[i].Name,studentInfo[i].Score);
        }
    }
    printf("=========================
");
    fclose(file);
}

//增加
void insert();
void insert()
{
    if ((file = fopen("/Users/3022/Desktop/code_C/课后作业/h_09学生信息/stu.txt","rb+")) == NULL)
    {
        printf("打开文件失败");
    }
    
    int m = 0;
    for (int i = 0; fread(&studentInfo[i], sizeof(StudentInfo), 1, file); i++)
    {
        m++;
    }
    
    createDataBase2(m);
    fclose(file);
}
//删除
void delete();
void delete()
{
    printf("
请输入要删除的名字:
");
    char name[10];
    scanf("%s",name);
    //
    if ((file = fopen("/Users/3022/Desktop/code_C/课后作业/h_09学生信息/stu.txt","rb+")) == NULL)
    {
        printf("打开文件失败");
    }
    
    int m = 0;
    for (int i = 0; fread(&studentInfo[i], sizeof(StudentInfo), 1, file); i++)
    {
        m++;
    }
    
    int loc = 0;
    for (int i = 0; i < m; i++)
    {
        if (strcmp(name,studentInfo[i].Name) == 0)
        {
            loc = i;
            break;
        }
    }
    
    for (int i = loc; i < m; i++)
    {
        strcpy(studentInfo[i].Id,studentInfo[i+1].Id);
        strcpy(studentInfo[i].Name,studentInfo[i+1].Name);
        studentInfo[i].Score = studentInfo[i+1].Score;
    }
    
    if ((file = fopen("/Users/3022/Desktop/code_C/课后作业/h_09学生信息/stu.txt","w+")) == NULL)
    {
        printf("打开文件失败");
    }
    
    for (int i = 0; i < m -1; i++)
    {
        if (fwrite(&studentInfo[i], sizeof(StudentInfo), 1, file) != 1)
        {
            printf("写文件失败");
        }
    }
    fclose(file);
    
}

int main(int argc, const char * argv[])
{
    int a = 0;
    printf("请输入1~5:1创建,2输出,3查找,4增加,5删除
");
    scanf("%d",&a);
    while (1)
    {
        switch (a)
        {
            case 1:
            {
                createDataBase();
                break;
            }
            case 2:
            {
                printStuInfo();
                break;
            }
            case 3:
            {
                checkStuInfo();
                break;
            }
            case 4:
            {
                insert();
                break;
            }
            case 5:
            {
                delete();
                break;
            }
            default:
                break;
        }
        
        //跳出条件
        printf("请输入1~5:1创建,2输出,3查找,4增加,5删除.其它则退出
");
        scanf("%d",&a);
        
        if((a >= 1 || a <=5))
        {
            continue;
        }
        else
        {
            break;
        }
        
        
    }
    
    return 0;
}
原文地址:https://www.cnblogs.com/huen/p/3499996.html