简体   繁体   English

在C中读取和写入

[英]fread and fwrite in C

I'm trying to make a program that creates and reads a binary file, which contains "struct elements"; 我正在尝试制作一个程序,该程序创建并读取一个包含“结构元素”的二进制文件; can you please tell me what I did wrong? 你能告诉我我做错了什么吗? I got errors telling me that "s" is not a pointer in function fread()... so I declared ELEM *s; 我有错误告诉我“ s”不是函数fread()中的指针...所以我声明了ELEM * s; instead of ELEM s; 代替ELEM;

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

typedef struct element{
    char name[80];
    int p;
}ELEM;

void create()
{
    FILE *f;
    int d=0;
    char c;
    ELEM *s;
    f=fopen("file.bin","wb");
    do{
    printf("Add elements to file?: (y/n)");
    fflush(stdin);
    scanf("%c",&c);
    if (c=='y')
    {
        printf("Name=");
        gets((*s).name);
        printf("P=");
        scanf("%d",(*s).p);
        fwrite(s,sizeof(ELEM),1,f);
    }
    } while(d==0);
    fclose(f);
}

void show()
{
    FILE *f;
    ELEM *s;
    f=fopen("file.bin","rb");
    while(feof(f)!=NULL)
    {
        fread(s,sizeof(ELEM),1,f);
        puts((*s).name);
        printf("\t%d\n",(*s).p);
    }
    fclose(f);
}

void add()
{
    FILE *f;
    int d=0;
    char c;
    ELEM *s;
    f=fopen("file.bin","ab");
    do{
    printf("Add elements to file?: (y/n)");
    fflush(stdin);
    scanf("%c",&c);
    if (c=='y')
    {
        printf("Name=");
        gets((*s).name);
        printf("P=");
        scanf("%d",(*s).p);
        fwrite(s,sizeof(ELEM),1,f);
    }
    } while(d==0);
    fclose(f);
}


/*void function()
{

}*/

int main()
{
    int k=0,r;
    do{
        printf("1 - create file\n2 - add elements to fil\n3 - show elements\n4 - put unique elements in another file\n5 - exit program\n");
        scanf("%d",&r);
        switch(r)
        {
            case 1 : create(); break;
            case 2 : add(); break;
            case 3 : show(); break;
            case 4 : printf("Function not defined!\n"); break;
            case 5 : k=1; break;
            default : printf("Command unrecognized!\n");
        }
    } while(k==0);
    return 0;
}

You declared a pointer but assigned no memory to it. 您声明了一个指针,但未为其分配任何内存。 You should revert to a normal variable: 您应该恢复为普通变量:

ELEM s;

/* ... */

fwrite(&s,sizeof(ELEM),1,f);
       ^

Alternatively, in your current code you should do this: 或者,在您当前的代码中,您应该这样做:

ELEM *s = calloc(1, sizeof *s);

The first parameter passed to fwrite should be an address, and the variable whose address you pass must have enough memory to hold the number of objects you plan to read. 传递给fwrite的第一个参数应该是一个地址,并且传递给其地址的变量必须具有足够的内存以容纳计划读取的对象数。

So there are two ways: 因此,有两种方法:

Creating Variable on Stack: 在堆栈上创建变量:

You allocate the variable on stack and pass its address to fwrite 您在堆栈上分配变量并将其地址传递给fwrite

ELEM s;
fwrite(&s,sizeof(ELEM),1,f);

or 要么

Dynamic Memory allocation: 动态内存分配:

ELEM *s;   

Should be allocated an memory equivalent to hold no of objects of type ELEM that you want to read. 应该分配一个等效于不容纳要读取的ELEM类型对象的内存的内存。

ELEM *s = malloc(sizeof *s);

In this case remember to free the memory once done with your use: 在这种情况下,请记住在使用后释放内存:

free(s);

You are not allocating the pointer s in your create function. 你是不是分配指针screate功能。 You probably want something like 您可能想要类似

 s = malloc(sizeof(*s));
 memset (s, 0, sizeof(*s));

etc. 等等

And you really should learn to compiler with gcc -Wall -g and to use the gdb debugger. 而且,您确实应该学习使用gcc -Wall -g进行编译以及使用gdb调试器。

Also, take time to read a good book about programming in C. 另外,请花时间阅读有关C语言编程的好书。

Well, your problems is in the scanf. 好吧,你的问题出在scanf上。 scanf should get a pointer, so change (*s).p to &(*s).p (or even &s->p ) scanf应该获得一个指针,因此将(*s).p更改为&(*s).p (甚至是&s->p

You should have allocated the pointer to struct ELEM Assuming this is C: 您应该已将指针分配给struct ELEM假设这是C:

 ELEM *s = malloc(sizeof(ELEM));

If it is C++ just add a cast in front 如果是C ++,只需在前面添加一个强制转换

 ELEM *s = (ELEM*) malloc(sizeof(ELEM));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM