如何将结构体数组传递给函数?读取艺术家数据代码重构
Let’s split your existing code into modular functions to clean up your main file. This makes your code easier to maintain, read, and reuse elsewhere in your program. Here’s a step-by-step solution:
1. Define Your Struct (and Consider a Header File)
First, keep your cantanti struct definition (you can move this to a separate header file like artisti.h later if you want to use it across multiple files):
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <time.h> #define max 30 #define maxu 20 #define sessomu 6 #define sessom 6 struct cantanti { int id; char nome[max], cognome[max], nazionalita[max], sesso[sessom], genere[max]; };
2. Create a Function to Load Artists from File
This function handles opening the file, reading the artist count, populating the struct array, and closing the file. It returns the number of artists loaded (or -1 if there’s an error like a missing file):
int loadArtists(struct cantanti array[], const char *filename) { FILE *fp = fopen(filename, "r"); if (fp == NULL) { printf("Errore: Impossibile aprire il file %s\n", filename); return -1; } int nartisti; fscanf(fp, "%d", &nartisti); for (int i = 0; i < nartisti; i++) { fscanf(fp, "%d %s %s %s %s %s", &array[i].id, array[i].nome, array[i].cognome, array[i].nazionalita, array[i].sesso, array[i].genere); } fclose(fp); return nartisti; }
3. Create a Function to Print the Artist List
Move all the printing logic into its own function to separate data loading from presentation:
void printArtists(struct cantanti array[], int count) { printf(" _____________________________________________________________________________________\n"); printf("|| %-5s || %-12s || %-12s || %-15s || %-4s || %-12s ||\n", "<ID>", "<NOME>", "<COGNOME>", "<NAZIONALITA'>", "<SESSO>", "<GENERE>"); printf("||_______||______________||______________||_________________||_________||______________||\n"); for (int i = 0; i < count; i++) { printf("||%-6d || %-12s || %-12s || %-15s || %-7s || %-12s ||\n", array[i].id, array[i].nome, array[i].cognome, array[i].nazionalita, array[i].sesso, array[i].genere); printf("||_______||______________||______________||_________________||_________||______________||\n"); } printf("**************************| Sono stati letti %d autori dal file |****************\n", count); printf("***************************\\___________________________________________/ ****************\n\n"); }
4. Simplify Your Main Function
Now your main code’s case 1 becomes much shorter and cleaner:
int main() { struct cantanti arrayc[100]; int choice; // ... (your other menu logic here) switch(choice) { case 1: { system("cls"); int nartisti = loadArtists(arrayc, "artisti.dat"); if (nartisti != -1) { printArtists(arrayc, nartisti); } system("pause"); system("cls"); break; } // ... (other cases here) } return 0; }
Key Improvements
- Modularity: Each function has a single responsibility (loading data vs printing), making your code easier to debug and modify.
- Error Handling: Added a check for failed file openings, which your original code didn’t have.
- Maintainability: If you need to change how artists are loaded or printed later, you only modify one function instead of digging through a huge main block.
内容的提问来源于stack exchange,提问作者Giacomo Corcella

