从二进制文件中读取mpq_t

时间:2015-06-18 05:35:46

标签: c fread gmp

我已经将mpq_t写入了一个名为“data”的二进制文件,现在我试图逐个从文件中读取mpq_t,但我仍然在这行继续使用segfault:gmp_printf(“%Qd \ n”,buf )。我已经调试了一段时间,但无法弄清楚出了什么问题。

int main(){

    FILE *fp = fopen("data", "rb");
    if (fp == NULL){
            perror("FILE open failed");
            exit(1);
    }

    mpq_t buf;
    mpq_init(buf);

    while (fread(&buf, sizeof(mpq_t), 1, fp) == 1){
            gmp_printf("%Qd\n", buf);
    }

    fclose(fp);
    return 0;
}

从文件中读取后,似乎我遇到了buf的内存问题。我也尝试过mallocing而不是启动,但它也没有用。

    mpq_t *buf = (mpq_t *)malloc(sizeof(mpq_t));

    if (buf == NULL){
            perror("malloc failed");
            exit(1);
    }

    while (fread(buf, sizeof(mpq_t), 1, fp) == 1){
            gmp_printf("%Qd\n", *buf);
    }

1 个答案:

答案 0 :(得分:0)

使用gmp_fprintf和gmp_fscanf来编写和阅读:

class GamesOverview: Interface, UITableViewDataSource, UITableViewDelegate{

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if (segue.identifier == "presentGame") {
            var presentGame = segue.destinationViewController as! Game;
            presentGame.gameState = self.gameState;
            println(presentGame.gameState); 
        }
    }
}