我想对我的保存功能有所帮助 - 将结构保存在我选择调用它的文件中。
所以我的代码是:
#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
出错,所以该怎么办?
答案 0 :(得分:1)
好的,我准备好了。我会写它类似于:
int save (struct students *h, int num_students){
char name[1024];
printf("Enter file name: " );
scanf("%s", name); // Read in filename
FILE *output = fopen(name, "w"); // open the file to write
if (!output) {
return -1; // error
}
for (int i = 0; i < num_students; ++i) {
// write a line - see fprintf documentation
fprintf(output, "%s %s %s %s\n", h[i].f_name, h[i].last, h[i].s,h[i].g);
// I'm missing some error checks here - you should not
}
fclose(output); // close
return 0;
}
然后通过传入学生数组和数组中的学生数量来调用它。我希望这至少可以帮助你。
答案 1 :(得分:0)
scanf("%s", name);
您在char name
中输入字符串。如果传递打开的输出文件,则不需要此name
。或者,如果您需要在此处打开文件,则需要将name
作为char缓冲区传递,例如char name[MAX_FILE_NAME_LEN];
答案 2 :(得分:0)
我认为该脚本缺少一些&符号&amp;'
scanf("%s", name); >> scanf("%s", &name); ?