1. 程式人生 > >結構體陣列操作+檔案讀寫

結構體陣列操作+檔案讀寫

#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("請錄入學生資訊:\n"); //建立學號 do { printf("請輸入學號:\n"); char tmpId[4]; scanf("%s",tmpId); if (strlen(tmpId) > 4) { flag
= 1; printf("學號輸入錯誤,請重新輸入。。。\n"); } else { flag = 0; strcpy(studentInfo[m].Id, tmpId); } } while (flag); // 建立姓名 do { printf("請輸入姓名。。。\n");
char tmpName[10]; scanf("%s",tmpName); if ((strlen(studentInfo[m].Name) <= 0) && (strlen(studentInfo[m].Name) > 10)) { flag = 1; printf("請輸入姓名。。。\n"); } else { flag = 0; strcpy(studentInfo[m].Name, tmpName); } } while (flag); // 錄入成績 do { printf("請輸入成績。。。\n"); scanf("%d",&studentInfo[m].Score); if ((studentInfo[m].Score > 100)&&(studentInfo[m].Score < 0 )) { flag = 1; printf("請重新輸入成績。。。\n"); } else { flag = 0; } } while (flag); len++; printf("是否繼續錄入,請按y或Y繼續\n"); char ch; getchar();//截取回車 scanf("%c",&ch); //printf("%c\n",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個學生資訊:\n",i+1); //建立學號 do { printf("請輸入學號:\n"); char tmpId[4]; scanf("%s",tmpId); if (strlen(tmpId) > 4) { flag = 1; printf("學號輸入錯誤,請重新輸入。。。\n"); } else { flag = 0; strcpy(studentInfo[i].Id, tmpId); } } while (flag); // 建立姓名 do { printf("請輸入姓名。。。\n"); char tmpName[10]; scanf("%s",tmpName); if ((strlen(studentInfo[i].Name) <= 0) && (strlen(studentInfo[i].Name) > 10)) { flag = 1; printf("請輸入姓名。。。\n"); } else { flag = 0; strcpy(studentInfo[i].Name, tmpName); } } while (flag); // 錄入成績 do { printf("請輸入成績。。。\n"); scanf("%d",&studentInfo[i].Score); if ((studentInfo[i].Score > 100)&&(studentInfo[i].Score < 0 )) { flag = 1; printf("請重新輸入成績。。。\n"); } else { flag = 0; } } while (flag); i++; m++; printf("是否繼續錄入,請按y或Y繼續\n"); char ch; getchar();//截取回車 scanf("%c",&ch); //printf("%c\n",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("\n所有的資訊:\n"); 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("讀檔案失敗\n"); } } printf("學號======姓名======成績===\n"); for (int i = 0 ; i < m; i++) { printf("%-8s %-8s %-8d\n",studentInfo[i].Id,studentInfo[i].Name,studentInfo[i].Score); } printf("=========================\n"); fclose(file); } //按名字查詢 void checkStuInfo(); void checkStuInfo() { printf("\n按名字查詢為:\n"); 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("讀檔案失敗\n"); } } printf("學號======姓名======成績===\n"); for (int i = 0; i < m; i++) { if (strcmp(name,studentInfo[i].Name) == 0) { printf("%-8s %-8s %-8d\n",studentInfo[i].Id,studentInfo[i].Name,studentInfo[i].Score); } } printf("=========================\n"); 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("\n請輸入要刪除的名字:\n"); 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刪除\n"); 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刪除.其它則退出\n"); scanf("%d",&a); if((a >= 1 || a <=5)) { continue; } else { break; } } return 0; }