阅读文件和填充结构 [英] Reading file and populating struct

查看:113
本文介绍了阅读文件和填充结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下定义的结构:

  typedef结构MYSTRUCT {
    int类型的;
    字符* C;
    INT F;
}对象;

我能够填充此对象,并将其写入文件。但是我不能够读取它的char * C值...尝试读取它,它给了我一个分段错误。有什么毛病我code:

  // writensave.c#包括mystruct.h
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#定义P(x)的printf(X)诠释的main()
{
    P(创建文件中写入... \\ n);
    FILE *文件=的fopen(struct.dat,W);
    如果(文件== NULL)
    {
        的printf(打开文件时出错\\ n);
        返回-1;
    }    P(创造结构\\ n);
    OBJECT * MyObj中=(OBJECT *)malloc的(sizeof的(对象));
    myObj->一种= 20;
    myObj-> F = 45;
    myObj-> C =(字符*)释放calloc(30,sizeof的(炭));
    的strcpy(myObj-&℃,
        这是一个测试);
    P(写入对象文件... \\ n);
    的fwrite(MyObj中,sizeof的(对象)时,1,文件);
    P(关闭档案\\ n);
    FCLOSE(文件);
    P(计划\\ n结束);
    返回0;
}

下面是如何,我想阅读:

  // readnprint.c
#包括mystruct.h
#包括LT&;&stdio.h中GT;
#定义P(x)的printf(X)
诠释的main()
{
    FILE *文件=的fopen(struct.dat,R);
    字符*缓冲区;
    缓冲=(字符*)malloc的(sizeof的(对象));
    如果(文件== NULL)
    {
        P(错误打开文件);
        返回-1;
    }    的fread((无效*)缓冲液,sizeof的(对象)时,1,文件);
    OBJECT * OBJ =(OBJECT *)缓冲;
    输出(obj->一种=%d个\\ nobj-> F =%d个\\ nobj-> C =%S,
        obj->一种,
        obj-> F,
        obj-> C);
    FCLOSE(文件);
    返回0;
}


解决方案

您最初的code样品似乎假定字符串将不超过30个字符大。如果是这样的话,那么最简单的解决方法是可能重新定义你的结构是这样的:

  typedef结构MYSTRUCT {
    int类型的;
    CHAR [30];
    INT F;
}对象;

否则,你只是存储一个指向动态分配的内存将被毁坏了,当你的程序退出(所以,当你取得这个指针后,该地址是不值钱的,最有可能非法访问)。

I have a structure with the following definition:

typedef struct myStruct{
    int a;
    char* c;
    int f;
} OBJECT;

I am able to populate this object and write it to a file. However I am not able to read the char* c value in it...while trying to read it, it gives me a segmentation fault error. Is there anything wrong with my code:

//writensave.c

#include "mystruct.h"
#include <stdio.h>
#include <string.h>


#define p(x) printf(x)

int main()
{
    p("Creating file to write...\n");
    FILE* file = fopen("struct.dat", "w");
    if(file == NULL)
    {
        printf("Error opening file\n");
        return -1;
    }

    p("creating structure\n");
    OBJECT* myObj = (OBJECT*)malloc(sizeof(OBJECT));
    myObj->a = 20;
    myObj->f = 45;
    myObj->c = (char*)calloc(30, sizeof(char));
    strcpy(myObj->c, 
        "This is a test");
    p("Writing object to file...\n");
    fwrite(myObj, sizeof(OBJECT), 1, file);
    p("Close file\n");
    fclose(file);
    p("End of program\n");
    return 0;       
}

Here is how I am trying to read it:

//readnprint.c
#include "mystruct.h"
#include <stdio.h>
#define p(x) printf(x)
int main()
{   
    FILE* file = fopen("struct.dat", "r");
    char* buffer;
    buffer = (char*) malloc(sizeof(OBJECT));
    if(file == NULL)
    {
        p("Error opening file");
        return -1;
    }

    fread((void *)buffer, sizeof(OBJECT), 1, file);
    OBJECT* obj = (OBJECT*)buffer;
    printf("obj->a = %d\nobj->f = %d \nobj->c = %s",
        obj->a,
        obj->f,
        obj->c);
    fclose(file);
    return 0;
}

解决方案

Your first code sample seems to assume that the strings are going to be no larger than 30 characters. If this is the case, then the easiest fix is probably to re-define your structure like this:

typedef struct myStruct{
    int a;
    char c[30];
    int f;
} OBJECT;

Otherwise, you're just storing a pointer to dynamically-allocated memory that will be destroyed when your program exits (so when you retrieve this pointer later, the address is worthless and most likely illegal to access).

这篇关于阅读文件和填充结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆