分段在C中写入文件时出错

时间:2016-09-22 17:11:20

标签: c io segmentation-fault

每当我尝试写入文件时,我都会遇到分段错误。我无法访问可以告诉我自从我在学校以来它来自哪里的软件。如果有人能帮助我,那就太好了。

//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 个答案:

答案 0 :(得分:1)

您打开文件进行阅读而不是写入,并且您没有检查操作是否成功。尝试

import { Component } from '@angular/core';
import {service} from './service';
@Component({
  selector: 'my-app',

  template: `
    <p fetchHTML>Hello</p>

    <div *ngFor="let m of data" fetchHTML>{{m}}</div>
    `

})
export class AppComponent { 
  data=["one","two","three"];
}

答案 1 :(得分:-2)

当打电话给fopen时,我打开它进行阅读而不是写作。我实际上创建了文件,我可以把它留作“阅读”