io - Segmentation Fault when writing to a file in C -
whenever attempt write file, getting segmentation fault. don't have access software can tell me it's coming since i'm @ school. if me out 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();
you opened file reading instead of writing, , did not check action succeeded. try
fp = fopen("accountslist.txt", "w"); if(fp == null) { // out code exit(1); }
Comments
Post a Comment