保存機能を使用して、名前を選択して構造をファイルに保存する方法を教えてください。
私のコードは次のとおりです。
#include <stdio.h>
#define MAX_NAME_LENGTH 100
#define MAX_STRUCTS 100
struct students
{
char name[MAX_NAME_LENGTH];
char last[MAX_NAME_LENGTH];
char g[MAX_NAME_LENGTH];
char s[MAX_NAME_LENGTH];
};
/* Load from the named file, into the `analg` array provided, but no more than `max` entries */
/* Returns the number of entries loaded */
int load(char *filename, struct students *h, int max)
{
int count = 0; /* The number of entries we have loaded */
FILE *fp = fopen(filename, "r");
char line[MAX_STRUCTS * 4]; /* *4 for first and last name grade and score*/
if (fp == NULL)
return; /* File could not be opened */
/* Read all lines, but only as long as we have available entries in the array */
while (count < max && fgets(line, sizeof(line), fp) != NULL)
{
/* Extract the first and last names from the newly read line */
sscanf(line, "%s %s %s %s", h[count].name, h[count].last, h[count].g, h[count].s);
count++; /* Increase counter so we have the current size */
}
/* All done loading */
fclose(fp);
return count; /* Return the number of entries we loaded */
}
/* Print from the structure array, there are `count` entries in the array */
void print(struct students *h, int count)
{
int i;
for (i = 0; i < count; i++)
{
/* Print the number and the names */
/* +1 to the index, because it starts from zero and we want to print it nicely to the user and start from 1 */
printf("%2d) %s %s %s %s\n", i + 1, h[i].name, h[i].last, h[i].g, h[i].s);
}
}
int save (struct students *h, FILE *oput, char name){
printf("Enter file name: " );
scanf("%s", name);
fwrite(h.f_name, h.last, h.s,h.g ,sizeof(struct students),1,oput);
}
int main(void)
{
struct students h[MAX_STRUCTS];
FILE *oput
char choice;
int count = 0; /* Initialize to zero, in case user chooses `print` first */
char filename[100];
int coun1t; /* The current number of entries in the array */
int remove; /* The entry to remove (index into array, so zero based) */
/* Move the still valid entries one step "down" in the array */
char line[MAX_STRUCTS * 4];
char name;
do
{
printf("choose L for load , P for print or S for save: \n");
/* Read input from user, as a character, while skipping leading and trailing whitespace */
scanf("%c", &choice);
switch (choice)
{
case 'l':
printf("file name? : ");
scanf("%s", &filename);
count = load(filename, h, MAX_STRUCTS);
if (count == 0)
printf("No structures loaded\n");
else (
printf("Data loaded\n")
);
break;
case 'p':
print(h, count);
break;
case 's':
count = save(h, name, oput);
break;
case 'q':
break;
default:
break;
}
} while ((choice) != 'q');
return 0;
}
私のテキストファイルで:
1)Joe Fanskol 10,4 100
2)Marina Jake 15.5 99
プログラムを実行できません。エラーが発生しint save
ています。どうすればよいですか?