ファイルに書き込もうとするたびに、セグメンテーションフォルトが発生します。私は学校に通ってからどこに来ているのか教えてくれるソフトウェアにアクセスすることはできません。もし誰かが私を助けてくれるなら、それはすばらしいだろう。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();
入力内容は? –
書き込み用ではなく、読み込み用にファイルを開いた。そして、常に 'fopen()'からの戻り値をチェックしてください。 –
Cでのmallocのキャストは悪い考えです - http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc –