保存数据 [英] Saving data

查看:33
本文介绍了保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个小函数来保存数据。基本上我有一个

大的2d结构数组,所以我将不得不在数组中每个结构调用一次save

函数。我想使用

fwrite()保存数据。经过相当多的努力我已经拿出了下面的代码

,但是我很难摆脱上一个错误。

编译时,我明白了:


第52行:警告:赋值使得整数指针没有

强制转换


我不确定是什么导致这种情况,但它非常令人困惑。任何人都可以帮助
帮助吗?


西蒙


- 下面的代码 -

#include< stdio.h>


struct test

{

char char1;

char char2;

char char3;

char char4;

int * pointer1;

int * pointer2;

};

main()

{

struct test test_st [3] [3 ];

int i,j;

for(j = 0; j< 3; j ++)

{

for(i = 0; i< 3; i ++)

{

test_st [j] [i] .char1 = 1;

test_st [j] [i] .char2 = 1;

test_st [j] [i] .char3 = 1;

test_st [j] [i] .char4 = i;

test_st [j] [i] .pointer1 = NULL;

test_st [j] [i] .pointer2 = NULL;

}

}

FILE * fp = fopen(" test.csd_saved_game"," w");

struct test * p;

p =& test_st [j] [i];

write_struct(fp,p);

/ * p -char1 ;

fwrite(p,1,1, fp);

fread(test_char,* /

返回0;

}

write_byte(FILE * fp ,unsigned char * temp,int bytes)

{

fwrite(temp,1,bytes,fp);

}

write_struct(FILE * fp,struct test * p)

{

unsigned char * temp;

temp = p -char1 ; <<<<<错误在这里

write_byte(fp,temp,sizeof(char));

}

I''m trying to write a little function to save data. Basically I have a
large 2d array of structs, so I''m going to have to call the save
function once per struct in the array. I''d like to save the data using
fwrite(). After quite a lot of effort I''ve come up with the code
below, but I''m having real difficulty getting rid of the last error.
When compiling, I get this:

Line 52: "warning: assignment makes pointer from integer without a
cast"

I''m not sure what''s causing this but it''s very confusing. Can anyone
help?

Simon

--Code below--

#include <stdio.h>

struct test
{
char char1;
char char2;
char char3;
char char4;
int* pointer1;
int* pointer2;
};
main()
{
struct test test_st[3][3];
int i, j;
for (j = 0; j < 3; j++)
{
for (i = 0; i < 3; i++)
{
test_st[j][i].char1 = 1;
test_st[j][i].char2 = 1;
test_st[j][i].char3 = 1;
test_st[j][i].char4 = i;
test_st[j][i].pointer1 = NULL;
test_st[j][i].pointer2 = NULL;
}
}
FILE *fp = fopen ("test.csd_saved_game", "w");
struct test *p;
p = &test_st[j][i];
write_struct (fp, p);
/* p -char1;
fwrite (p, 1, 1, fp);
fread (test_char, */
return 0;
}
write_byte (FILE *fp, unsigned char *temp, int bytes)
{
fwrite (temp, 1, bytes, fp);
}
write_struct (FILE *fp, struct test *p)
{
unsigned char *temp;
temp = p -char1; <<<<ERROR IS HERE
write_byte (fp, temp, sizeof(char));
}

推荐答案

西蒙说:


< snip>
Simon said:

<snip>

>

第52行:警告:赋值使整数指针没有

强制转换
>
Line 52: "warning: assignment makes pointer from integer without a
cast"



< snip>

<snip>


>

struct test

{

char char1;

char char2;

char char3;

char char4;

int * pointer1;

int * pointer2;

};
>
struct test
{
char char1;
char char2;
char char3;
char char4;
int* pointer1;
int* pointer2;
};



< snip>

<snip>


>

write_struct(FILE * fp,struct test * p)

{

unsigned char * temp;

temp = p -char1; <<<< ERROR IS HERE
>
write_struct (FILE *fp, struct test *p)
{
unsigned char *temp;
temp = p -char1; <<<<ERROR IS HERE



temp =(unsigned char *)& p -char1;

temp = (unsigned char *)& p -char1;


write_byte(fp,temp,sizeof(char));
write_byte (fp, temp, sizeof(char));



或者你可以这样做:write_byte(fp,(unsigned char *)& p-> char1,1);


并完全失去温度。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:正常服务将尽快恢复。请不要

调整您的电子邮件客户端。

Or you could just do this: write_byte(fp, (unsigned char *)&p->char1, 1);

and lose temp completely.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.



write_struct(FILE * fp,struct test * p)

{

unsigned char * temp;

temp = p -char1; <<<<<错误在这里

write_byte(fp,temp,sizeof(char));

}
write_struct (FILE *fp, struct test *p)
{
unsigned char *temp;
temp = p -char1; <<<<ERROR IS HERE
write_byte (fp, temp, sizeof(char));
}



好​​吧,p-> char1的类型是char但它被分配给一个

字符指针,这没有意义,它应该是

以下,


unsigned char temp;

temp = p -char1;





unsigned char * temp;

temp =&(p - > char1);

Well, type of p->char1 is char but it is being assigned to a
character pointer, that doesn''t make sense, it should be either one of
the following,

unsigned char temp;
temp = p -char1;

or

unsigned char *temp;
temp = &(p ->char1);


我正在慢慢取得进展,但我仍然在努力...


我现在可以成功写入文件,但我似乎无法获得与我投入的相同的价值。我已经复制了测试程序并且

下面的示例输出。我要做的就是将一个结构数组的内容保存到文件中,然后将它们加载回另一个结构数组。问题是我没有得到相同的价值

回来。我很担心我错过了一些基本的东西,所以任何

的解释都会非常感激。


提前致谢


Simon

----测试程序----


/ *测试保存和加载* /


#include< stdio.h>


struct test

{

char char1;

char char2;

char char3;

char char4;

int * pointer1;

int * pointer2;

};

main()

{

struct test test_st [3 ] [3];

struct test test_s2 [3] [3];

int i,j;

for(j = 0; j< 3; j ++)

{

for(i = 0; i< 3; i ++)

{

test_st [j] [i] .char1 = i;

test_st [j] [i] .char2 = j;

test_st [j] [i ] .char3 = i + j;

test_st [j] [i] .char4 = i * j;

test_st [j] [i] .pointer1 = NULL;

test_st [j] [i] .pointer2 = NULL;

}

}

printf(" Array initialised ... \ n");

for(i = 0;我< 3; i ++)

printf(" test_st [%d] [%d]:char1 =%d,char2 =%d,char3 =%d,char4 =

% d \ n",i,i,test_st [i] [i] .char1,test_st [i] [i] .char2,

test_st [i] [i] .char3,test_st [ i] [i] .char4);

FILE * fp = fopen(" test2.csd_saved_game"," w");

struct test * p;

for(j = 0; j< 3; j ++)

{

for(i = 0; i< 3; i ++)

{

p =& test_st [j] [i];

write_struct(fp,& p);

}

}

printf(" Data written ... \ n");

for(j = 0; j< 3; j ++)

{

for(i = 0; i< 3; i ++)

{

p =& test_s2 [j] [i];

read_struct(fp,& p);

}

}

for(i = 0; i< 3; i ++)

printf(" test_s2 [%d] [%d]:char1 =%d,char2 = %d,char3 =%d,char4 =

%d \ n",i,i,test_s2 [i] [i] .char1,test_s2 [i] [i] .char2,

test_ s2 [i] [i] .char3,test_s2 [i] [i] .char4);

返回0;

}

write_byte (FILE * fp,unsigned char * temp)

{

fwrite(temp,1,1,fp);

}

read_byte(FILE * fp,unsigned char * temp)

{

fread(temp,1,1,fp);

}

write_struct(FILE * fp,struct test * p)

{

unsigned char * temp;

temp =&(p -char1);

write_byte(fp,temp);

temp =&(p -char2);

write_byte(fp,temp);

temp =&(p -char3);

write_byte(fp,temp);

temp =&(p -char4);

write_byte(fp,temp);

}

read_struct(FILE * fp,struct测试* p)

{

unsigned char * temp;

temp =&(p -char1);

read_byte(fp,temp);

temp =&(p -char2);

read_byte(fp,temp);

temp =&(p -char3);

read_byte(fp,temp);

tem p =&(p -char4);

read_byte(fp,temp);

}

----样本输出 - -


数组初始化...

test_st [0] [0]:char1 = 0,char2 = 0,char3 = 0,char4 = 0

test_st [1] [1]:char1 = 1,char2 = 1,char3 = 2,char4 = 1

test_st [2] [2]:char1 = 2 ,char2 = 2,char3 = 4,char4 = 4

写入数据......

test_s2 [0] [0]:char1 = 4,char2 = 0,char3 = 0,char4 = 0

test_s2 [1] [1]:char1 = -104,char2 = 55,char3 = 61,char4 = 0

test_s2 [2] [2]:char1 = -21,char2 = 6,char3 = -111,char4 = 124

I''m slowly making progress, but I''m still struggling with this...

I can now write successfully to a file, but I can''t seem to get the
same values back that I''ve put in. I''ve copied the test program and
the sample output below. All I''m trying to do is to save the contents
of one array of structs to a file and then load them back into another
array of structs. The problem is that I''m not getting the same values
back. I''m worried that I''m missing something fundamental, so any
explanation would be really appreciated.

Thanks in advance

Simon
---- test program ----

/* Testing saving and loading */

#include <stdio.h>

struct test
{
char char1;
char char2;
char char3;
char char4;
int* pointer1;
int* pointer2;
};
main()
{
struct test test_st[3][3];
struct test test_s2[3][3];
int i, j;
for (j = 0; j < 3; j++)
{
for (i = 0; i < 3; i++)
{
test_st[j][i].char1 = i;
test_st[j][i].char2 = j;
test_st[j][i].char3 = i + j;
test_st[j][i].char4 = i * j;
test_st[j][i].pointer1 = NULL;
test_st[j][i].pointer2 = NULL;
}
}
printf ("Array initialised...\n");
for (i = 0; i < 3; i++)
printf ("test_st[%d][%d]: char1 = %d, char2 = %d, char3 = %d, char4 =
%d\n", i, i, test_st[i][i].char1, test_st[i][i].char2,
test_st[i][i].char3, test_st[i][i].char4);
FILE *fp = fopen ("test2.csd_saved_game", "w");
struct test *p;
for (j = 0; j < 3; j++)
{
for (i = 0; i < 3; i++)
{
p = &test_st[j][i];
write_struct (fp, &p);
}
}
printf ("Data written...\n");
for (j = 0; j < 3; j++)
{
for (i = 0; i < 3; i++)
{
p = &test_s2[j][i];
read_struct (fp, &p);
}
}
for (i = 0; i < 3; i++)
printf ("test_s2[%d][%d]: char1 = %d, char2 = %d, char3 = %d, char4 =
%d\n", i, i, test_s2[i][i].char1, test_s2[i][i].char2,
test_s2[i][i].char3, test_s2[i][i].char4);
return 0;
}
write_byte (FILE *fp, unsigned char *temp)
{
fwrite (temp, 1, 1, fp);
}
read_byte (FILE *fp, unsigned char *temp)
{
fread (temp, 1, 1, fp);
}
write_struct (FILE *fp, struct test *p)
{
unsigned char *temp;
temp = &(p -char1);
write_byte (fp, temp);
temp = &(p -char2);
write_byte (fp, temp);
temp = &(p -char3);
write_byte (fp, temp);
temp = &(p -char4);
write_byte (fp, temp);
}
read_struct (FILE *fp, struct test *p)
{
unsigned char *temp;
temp = &(p -char1);
read_byte (fp, temp);
temp = &(p -char2);
read_byte (fp, temp);
temp = &(p -char3);
read_byte (fp, temp);
temp = &(p -char4);
read_byte (fp, temp);
}
---- sample output ----

Array initialised...
test_st[0][0]: char1 = 0, char2 = 0, char3 = 0, char4 = 0
test_st[1][1]: char1 = 1, char2 = 1, char3 = 2, char4 = 1
test_st[2][2]: char1 = 2, char2 = 2, char3 = 4, char4 = 4
Data written...
test_s2[0][0]: char1 = 4, char2 = 0, char3 = 0, char4 = 0
test_s2[1][1]: char1 = -104, char2 = 55, char3 = 61, char4 = 0
test_s2[2][2]: char1 = -21, char2 = 6, char3 = -111, char4 = 124


这篇关于保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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