为什么我的写入函数无法正确写入 .bin 文件?

表观

我正在编写一组函数以编写二进制文件,然后读取它并打印内容。结构必须是 1 个字节,并且每个成员必须存储一个 0-15 的数字 - 总共 8 位)(由我在 main() 中硬编码)。我遇到的问题是我的写入函数没有将正确的数字存储在它创建的文件中,因为我将这些数字硬编码到结构中。我一直在试图解决这个问题,但我不确定问题是什么......我有另一个程序可以正常工作,目的非常相似。请参阅下面的代码和我得到的输出。我也有一些试图解决它的打印语句。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* Purpose:
            */

struct score{
  //max 15 values for each
  //4 bits for player1 and 4 for player2
  unsigned char player1:4;
  unsigned char player2:4;

};

void printGG(struct score* game){
    printf("%hhu:%hhu\n", game->player1 , game->player2);
}

int writeGG(struct score* game, char * fileName){

    FILE *fp;
    if (!(fp=fopen(fileName,"wb"))){
      return 1;//file open error
    }
    //print to test the game pointer
    printf("WRITEGG() : player1: %d, player2: %d\n", game->player1, game->player2 );

    fwrite(&game ,sizeof(struct score) ,1 , fp);//write to file

    // check if fwrite failed
    if (fp == NULL){
        fprintf(stderr,"fwrite failed on %s\n",fileName);
        exit(EXIT_FAILURE);//can't write
    }
    fclose(fp);
    return 0;//normal termination
}

//reads provided .bin file and populates a score struct, then prints it.

int readGG(char *fileName)
{
    FILE *fp;
    if (!(fp=fopen(fileName,"rb"))){
        fprintf(stderr,"failed to open %s\n",fileName);
        exit(EXIT_FAILURE);//can't open
    }
    struct score s;
    if(fread(&s ,sizeof(struct score) ,1 , fp)!=1) return 2;//read error
    printf("readGG() : player1: %d, player2: %d\n", s.player1, s.player2 );
    fclose(fp);
    printf("%hhu:%hhu\n",s.player1,s.player2);
    return 0;//normal termination
}



int main()
{
    //create the score struct
    struct score scores;

    //hard code the scores
    scores.player1 = 10;
    scores.player2 = 1;
    printf("      player 1 has: %d\n",scores.player1);
    printf("      player 2 has: %d\n",scores.player2);

    //print the score in the format score:score
    printGG(&scores);

    //write to bin file
    writeGG(&scores, "Score.bin");

   //read file 
    readGG("Score.bin");

    
    return 0;
}

我觉得这可能只是我遗漏的一个小错误,但我只是看不到它。

//Output for the main() when ran:

      player 1 has: 10
      player 2 has: 1
10:1
WRITEGG() : player1: 10, player2: 1
15:1

//here the .bin file contains just the letters 1F...which is 15 and 1,
//not 10 and 1 which i have entered.
pm100

你正在写出地址struct score *game

 fwrite(&game ,sizeof(struct score) ,1 , fp);//write to file

游戏已经是一个指针struct score你需要

fwrite(game ,sizeof(struct score) ,1 , fp);//write to file

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我的BST无法写入文件?

为什么我的StreamWriter无法写入文件?

为什么我的文件目录包含“ / bin”?

为什么我无法获取文件路径并写入db?

为什么我无法在python 3中写入文件?

无法提取.bin文件

为什么我的FileOutputstream不写入文件?

为什么我的代码未写入文件?

为什么拒绝我创建的bin文件的执行权限?

为什么在写入文件时删除空白无法读取文件?

PHP函数无法正确写入文件,遗漏了选择字段

为什么数据没有正确写入/读取此文件?

* .xlsx文件何时以及为什么包含* .bin文件?

为什么root无法打开文件进行写入?

为什么我要写入的文件“不存在”?

为什么我仍然可以写入标有400的文件

为什么我的Out-File不将输出写入文件

为什么我的代码没有写入 .ini 文件?

为什么我的 Python 文件不在 CSV 中写入多行?

为什么我的代码没有写入我在写入 CSV 时指示的文件路径?

使用BinarySearch算法的C ++函数(.bin文件)

无法在bin文件上运行chmod

这个文件是什么:/ usr / bin / [?

我想使用Flink的流式文件接收器写入ORC文件,但是它无法正确写入文件

FileOutputStream无法正确写入非.txt文件

Python 3.6无法正确写入csv文件

无法在 Json 文件上写入正确的值

为什么我的 Lambda 函数会向 S3 写入一个空的 csv 文件?

为什么我必须进入我的私人bin文件夹才能执行.sh脚本?