【问题标题】:FILE I/O operations. Clearing the elements of an structure文件 I/O 操作。清除结构的元素
【发布时间】:2017-01-31 15:09:59
【问题描述】:

我是一个初学者,但我基本上是在创建一个程序,它打开一个包含“部件”结构的二进制文件,读取结构 放入一个数组,将每个结构的 on_hand 成员设置为 0,然后将结构写回文件。这是我的代码:

invclear.c

/* Modifies a file of part records by setting the quantity
   on hand to a zero for all records */

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

#define NAME_LEN 25
#define MAX_PARTS 100

struct part {                //size= 36 bytes (2 holes in between number and name[] array
    int number;
    char name[NAME_LEN+1];
    int on_hand;
    }inventory[MAX_PARTS];               

int num_parts;

int main()
{
    FILE *fp;
    int i;

    if((fp=fopen("clear_sample.c", "rb+")) == NULL)
    {
        fprintf(stderr, "Can't open inventory file.\n");
        exit(EXIT_FAILURE);
    }

    num_parts = fread(inventory, sizeof(struct part), MAX_PARTS, fp);    //reads the contents


    for(i=0; i<num_parts; i++)
        inventory[i].on_hand=0;         //clears them

    rewind(fp);                         //sets file position at beggining

    fwrite(inventory,sizeof(struct part), num_parts, fp); 
    fclose(fp);

    return 0;
}

clear_sample.c

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

#define NAME_LEN 25
#define MAX_PARTS 100

struct part {
    int number;
    char name[NAME_LEN+1];
    int on_hand;
    }inventory[MAX_PARTS]={0};


int main()
{
    int i;
    int num_parts;

    for(i=1;i<=15;i++)
        inventory[i].on_hand=i;

    for(i=1;i<=15;i++)
        printf("%d\n",inventory[i].on_hand);

    return 0;
}

它运行没有错误,但不幸的是 它没有将 on_hand 变量清除为 0,实际上它几乎删除了整个文件。这是我得到的修改后的 clear_sample.c 文件:

#include <stdio.h>
#include <st

任何关于我为什么做错的想法将不胜感激。

【问题讨论】:

  • 你到底为什么要使用一个明显不包含数据的C源文件作为二进制数据文件?当您读取一个在字节 34 处显式具有“0”的二进制文件作为文本文件时,您认为会发生什么? (提示:看看文件有多大)
  • 所有文件都是二进制文件。如果您只在其中放入可打印的字符,则某些二进制文件作为文本是有意义的。如果您将任意二进制数据写入文件,这通常不是您所得到的。不要将 C 源文件用作数据文件。创建一个专用的二进制数据文件,将“clear_sample.c”换成“data.bin”之类的。您可能需要确保它足够大以存放您的 100 个零件
  • 顺便说一句,在 32 位架构上,此结构:{ int, char[26], int } 在第一个 int 和 char 数组之间不会有空洞。数组和第二个 int 之间会有一个 2 字节的空洞。您可以“免费”将您的姓名长度增加到 27 个字符。
  • 你应该再读一遍你的作业,我确定你误解了一些东西。一定有一个二进制文件在你应该阅读的地方,或者你可能需要自己创建一个。
  • 当然。二进制文件并不特殊。任何随机的 3600 字节(你可以从 /dev/random 获得)都会生成一个文件,你的程序会很乐意将其读取为 100 部分记录。当然,单个零件数据绝对是垃圾,但您的程序不会在意。 (你可能)。尝试将 3600 'X' 写入数据文件,运行程序,然后在十六进制编辑器中打开文件

标签: c file fwrite fread


【解决方案1】:

您被用于检查结果文件的任何工具误导了。不是

#include <stdio.h>
#include <st

其实是这样的

#include <stdio.h>
#include <std^@^@^@^@h>

#define NAME_LEN 25
#define ^@^@^@^@PARTS 100

struct part {
    int^@^@^@^@ber;
    char name[NAME_LEN+1];
^@^@^@^@int on_hand;
    }inventory[MAX_^@^@^@^@S]={0};


int main()
{
    int i^@^@^@^@  int num_parts;

    for(i=1;i<^@^@^@^@i++)
        inventory[i].on_han^@^@^@^@

    for(i=1;i<=15;i++)
       ^@^@^@^@ntf("%d\n",inventory[i].on_hand)^@^@^@^@   return 0;
}

所有^@ 都是二进制零字节,对应于您放入on_hand 的零。

如 cmets 中所述,要测试您的程序,请准备一个符合您期望格式的文件。

【讨论】:

  • 好消息是,一旦他有了合适的数据文件,他的程序可能会工作。
猜你喜欢
  • 2011-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-27
相关资源
最近更新 更多