Ich schrieb eine verknüpfte Liste und schaffte es, filesupport hinzuzufügen. Jetzt gibt es ein Problem mit der Eingabeaufforderung. Die letzte pokemon->name
und pokemon->number
wird kryptisch ausgegeben. Irgendwie nehme ich an, dass ich einen Fehler beim Speichern des letzten Stapels von Daten im Speicher gemacht habe, weil er tatsächlich korrekt in der Datei gespeichert ist.Ausdruck der Eingabe einer verketteten Liste [C]
Hier ist der Code (Eingangsprüfung nach dem Code)
pokemonPtr addPokemon(void){
FILE *filePtr;
//filePtr = fopen ("pokedex.txt", "a");
//if (filePtr == NULL){
filePtr = fopen ("pokedex.txt","w");
//}
pokemonPtr firstPtr;
pokemonPtr thisPokemon;
firstPtr = NULL;
firstPtr = (pokemon *) malloc(sizeof(pokemon));
firstPtr->name = (char *) malloc(sizeof(char) * POKEMON_LENGTH);
printf ("Enter the name of the Pokemon.\n");
scanf("%s",firstPtr->name);
fprintf(filePtr, "Pokemon Name:%s ", firstPtr->name);
getchar();
printf ("Enter the number of the Pokemon.\n");
scanf("%d",&firstPtr->number);
fprintf(filePtr, "Pokemon Nummer:%d\n", firstPtr->number);
firstPtr->next = (pokemon *) malloc(sizeof(pokemon));
thisPokemon = firstPtr->next;
int i = 0;
while (i < 2){
thisPokemon->name = (char *) malloc(sizeof(char) * POKEMON_LENGTH);
printf ("Enter the name of the Pokemon.\n");
scanf("%s",thisPokemon->name);
fprintf(filePtr, "Pokemon Name:%s ", thisPokemon->name);
printf ("Enter the number of the Pokemon.\n");
scanf("%d",&thisPokemon->number);
fprintf(filePtr, "Pokemon Nummer:%d\n", thisPokemon->number);
thisPokemon->next =(pokemon *) malloc (sizeof(pokemon));
thisPokemon = thisPokemon->next;
i++;
}
thisPokemon->next = NULL;
fclose (filePtr);
return firstPtr;
}
void showPokemon(pokemonPtr firstPtr){
printf ("Name: %s\n"
"Nummer: %d\n", firstPtr->name, firstPtr->number);
pokemonPtr thisPokemon = firstPtr->next;
while (thisPokemon != NULL){
printf ("Name: %s\n"
"Nummer: %d\n", thisPokemon->name, thisPokemon->number);
thisPokemon = thisPokemon->next;
}
}
Der Eingang I versucht war:
Pokemon Name: dudu Pokemon Nummer: 3
Pokemon Name: dada Pokemon Nummer 3
Pokemon Name:: dudi Pokemon Nummer: 23
Die Ausgabe in cmd war:
Name: dudu
Nummer: 3
Name: dada
Nummer: 3
Name: dudi
Nummer: 23
Name: Ot
Nummer: 7607776
Was ist hier passiert?
oh sry, ich werde die fehlende Funktion hinzufügen –