Schreiben aus einer Struktur in eine Textdatei mit C

ich würde gerne etwas Hilfe mit meinem save-Funktion - zum speichern von Struktur in eine Datei, die ich wählen, was es aufgerufen werden soll.

also mein code ist :

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

in meiner text-Datei:

       1)Joe Fanskol     10,4     100

       2)Marina Jake     15.5     99

Kann ich nicht das Programm ausführen, es ist ein Fehler in int save also, was soll ich tun?

  • fwrite dauert nur 4 Argumente, die Sie liefern, mehr.
  • Auch sind Sie nicht fopening oput überall.
  • ok, aber ich brauche fopen'somthing, weil meine Datei ist bereits geöffnet in meinem Array in der Struktur. was ich also brauche, ist, wie zu bekommen, was ich haben in der Struktur, und kopieren oder schreiben Sie es in eine Textdatei
  • Ganz einfach: 1) Sie müssen die fopen() er (und vielleicht suchen, um den richtigen Datensatz-position 2) müssen Sie fwrite() (die richtige Menge der richtigen Daten), um Sie 3) Sie haben zu fclose() es. korrigiert
  • ok, ich mache wirklich undestand , aber was soll ich open()? und Close()? Ich meine, welche Datei oder was? - @wildplasser
  • gå, um die int save und Sie sehen meine Fehler code !!
  • Rufen Sie save() mit Argumenten falsch herum, und Sie brauchen die DATEI nicht öffnen *oput (das Sie vorbei sind, als char-name). Sie versuchen, Scannen Sie einen string in einen char, der Sie nichts anfangen. Es ist voller Fehler, die ich fürchte!

InformationsquelleAutor | 2013-03-03
Schreibe einen Kommentar