-2

Whenever I attempt to write to a file, I am getting a segmentation fault. I don't have any access to software that can tell me where it's coming from since I'm at school. If anyone could help me out that would be great.

//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();

2 Answers2

1

You opened the file for reading instead of writing, and you did not check the action succeeded. Try

fp = fopen("accountslist.txt", "w");
if(fp == NULL) {
    // get out code
    exit(1);
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
-2

When calling fopen, I was opening it for reading rather than writing. I physically created the file that way I can leave it as "reading"