Immer wenn ich versuche, in eine Datei zu schreiben, erhalte ich einen Segmentierungsfehler. Ich habe keinen Zugang zu Software, die mir sagen kann, woher es kommt, seit ich in der Schule bin. Wenn mir jemand helfen könnte, wäre das großartig.Segmentierung Fehler beim Schreiben in eine Datei in C
//OLMHash.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "OLMHash.h"
int main()
{
createAccount();
return 0;
}
struct account createAccount()
{
struct account *newAccount;
newAccount = (struct account *)malloc(sizeof(struct account));
printf("Enter userid: ");
scanf("%s", newAccount->userid);
printf("\nEnter password: ");
scanf("%s", newAccount->password);
char *output = malloc(sizeof(char) * 255);
strcpy(output, newAccount->userid);
strcat(output, "\t");
strcat(output, newAccount->password);
FILE* fp;
fp = fopen("accountslist.txt", "r");
fputs(newAccount->userid, fp);
free(output);
}
-
//OLMHash.h
struct account
{
char userid[32];
char password[12];
};
struct account createAccount();
Was ist Ihre Eingabe? –
Sie haben die Datei zum Lesen geöffnet, nicht zum Schreiben. Und bitte * immer * überprüfe den Rückgabewert von 'fopen()'. –
Casts für malloc in C ist eine schlechte Idee - http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc –