-1

我想对我的保存功能有一些帮助 - 将结构保存在我选择将要调用的文件中。

所以我的代码是:

#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 我该怎么办?

4

3 回答 3

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;
}

然后你通过传入你的学生数组和数组中的学生人数来调用它。我希望这至少对您有所帮助。

于 2013-03-03T22:28:08.073 回答
0
 scanf("%s", name); 

您将字符串输入到char name. name如果您传递打开的输出文件,则不需要这个。或者,如果您需要在此处打开文件,则需要name作为字符缓冲区传递,例如char name[MAX_FILE_NAME_LEN];

于 2013-03-03T22:29:30.697 回答
0

我认为该脚本缺少一些&符号'&'

scanf("%s", name); >> scanf("%s", &name); ?
于 2013-12-17T07:42:20.473 回答