473,837 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Saving data

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_save d_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));
}

Nov 11 '06 #1
14 2067
Simon said:

<snip>
>
Line 52: "warning: assignment makes pointer from integer without a
cast"
<snip>
>
struct test
{
char char1;
char char2;
char char3;
char char4;
int* pointer1;
int* pointer2;
};
<snip>
>
write_struct (FILE *fp, struct test *p)
{
unsigned char *temp;
temp = p -char1; <<<<ERROR IS HERE
temp = (unsigned char *)& p -char1;
write_byte (fp, temp, sizeof(char));
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.
Nov 11 '06 #2
write_struct (FILE *fp, struct test *p)
{
unsigned char *temp;
temp = p -char1; <<<<ERROR IS HERE
write_byte (fp, temp, sizeof(char));
}
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);

Nov 11 '06 #3
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_sav ed_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

Nov 11 '06 #4

"Simon" <ya*********@go oglemail.comwro te in message
news:11******** **************@ e3g2000cwe.goog legroups.com...
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_sav ed_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
Among other problems.
You need to heed your compiler warnings, one of them is causing
a bug.
Nov 12 '06 #5

Barry wrote:
Among other problems.
You need to heed your compiler warnings, one of them is causing
a bug.
I don't get any compiler warnings when I compile the program which is
why I'm finding the problem so difficult to solve. The second set of
data printed to the screen is not consistent - it's different each time
the program is run. That makes me think that fread() is not reading
the data in correctly, but I can't see what's wrong with my code.

Simon

Nov 12 '06 #6

"Simon" <ya*********@go oglemail.comwro te in message
news:11******** *************@i 42g2000cwa.goog legroups.com...
>
Barry wrote:
Among other problems.
You need to heed your compiler warnings, one of them is causing
a bug.

I don't get any compiler warnings when I compile the program which is
why I'm finding the problem so difficult to solve. The second set of
data printed to the screen is not consistent - it's different each time
the program is run. That makes me think that fread() is not reading
the data in correctly, but I can't see what's wrong with my code.

Simon
You need to crank up the warning level on your compiler. If it won't
spit out at least 10 you need a new compiler.

Barry
Nov 12 '06 #7

Simon wrote:
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.
Among other things, you are opening the file for writing, so
your reads will generally not work. Also, if you want to
read from the beginning of the file, you need to rewind the
file. You will also need to flush the file after you've done
the writes to ensure that the reads will work.

--
Bill Pursell

Nov 12 '06 #8

"Simon" <ya*********@go oglemail.comwro te in message
news:11******** *************@i 42g2000cwa.goog legroups.com...
>
Barry wrote:
Among other problems.
You need to heed your compiler warnings, one of them is causing
a bug.

I don't get any compiler warnings when I compile the program which is
why I'm finding the problem so difficult to solve. The second set of
data printed to the screen is not consistent - it's different each time
the program is run. That makes me think that fread() is not reading
the data in correctly, but I can't see what's wrong with my code.

Simon
Oops, I sent the message before I finished replying. Before you fread()
you need to get back to the start of the file. Look at rewind() or fseek().
Nov 12 '06 #9
Simon wrote:
Barry wrote:
>Among other problems.
You need to heed your compiler warnings, one of them is causing
a bug.

I don't get any compiler warnings when I compile the program which is
why I'm finding the problem so difficult to solve. The second set of
data printed to the screen is not consistent - it's different each time
the program is run. That makes me think that fread() is not reading
the data in correctly, but I can't see what's wrong with my code.
Well, if you tell your compiler to conform to a C standard (either one)
it is required to produce at least one diagnostic. What it diagnoses
will depend on the standard you choose. So, currently, your code is in a
language that is not quite C.

C90, the standard most commonly fully implemented, does not allow
declarations after the first statement in a block. I.e. the following is
not allowed:
{
int i;
i = 0;
int j;

In C99, the current standard which is implemented in only a very few
compilers (gcc has a C99 mode, but the GCC team admit that it is not yet
C99 conforming) then you have to at least declare function before you
use them. It also removes implicit int, so your declaration of
main()
is no longer legal. So, if we fix all these problems so you have
prototypes before you call the functions and do not declare variables
after a statement and don't use implicit int you get code any compiler
will complain about, even gcc without any options. I've also sorted the
indentation which was lost somewhere (don't use tabs for indentation
when posting to news groups) and declared functions as void where they
do not return anything:

/* Testing saving and loading */

#include <stdio.h>

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

void write_byte (FILE *fp, unsigned char *temp);
void read_byte (FILE *fp, unsigned char *temp);
void write_struct (FILE *fp, struct test *p);
void read_struct (FILE *fp, struct test *p);

int main(void)
{
struct test *p;
struct test test_st[3][3];
struct test test_s2[3][3];
int i, j;
FILE *fp = fopen ("test2.csd_sav ed_game", "w");
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);
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;
}
void write_byte (FILE *fp, unsigned char *temp)
{
fwrite (temp, 1, 1, fp);
}
void read_byte (FILE *fp, unsigned char *temp)
{
fread (temp, 1, 1, fp);
}
void 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);
}
void 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);
}

Now that I've got your code to the point where the compiler will
complain about it, fix the issues the compiler complains about. They are
serious errors. Also, find out how to get your compiler to conform to a
C standard and also enable such other warnings as it can provide. If
using gcc, for example, as a bare minimum compile with
gcc -ansi -pedantic -Wall

Please everyone note, the above code is not mine and I would never have
written it. If you quote it please make it clear that it is not my code.
--
Flash Gordon
Nov 12 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7531
by: G-Factor | last post by:
Hi all I've just started learning about saving files. I got bit of a problem. The following code gives me an error about incompatible types. (Cannot covert from class character to char *). I would appreciate it if anyone could either help me out, or direct me to an online resource site that has information on saving/loading classes. I have found several tutorials, but they either do not really help (saving text files) or are too...
4
10822
by: John Kandell | last post by:
Hi, I posted this in the asp.net group, but didn't get a response. Maybe someone here can help me with this... --- Would someone be able to shed some light on what is the cost of saving a DataTable to session vs saving a custom object of the same data.
4
3308
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any errors. After reading the ole object from db, I saved it to C: as file1.bmp and displayed on the web. But it can not be displayed. After I manually sent the file to wordpad, it shows
3
3631
by: JamesB | last post by:
I am saving image files from my website using the downloaddata on the web client. This gives me a byte array, which I am then saving to a file with the following code: ' Create the new, empty data file. If File.Exists(ImgDest) Then Console.WriteLine("{0} already exists!", ImgDest) Return End If Dim fs As New FileStream(ImgDest, FileMode.CreateNew)
4
6728
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my code ?? thank you Pedro Leite From Portugal ------------------------------------
10
2100
by: JoeC | last post by:
I am writing a game and all my game pieces are stored in a single vector of piece handles. I have the basics I can read and write char and number files but I am trying to do comthing more complicated. I am trying to save the data I need to save and I can fill in some when the necessary data is saved. Vector->piece handle -pointer to the pieces. How do I go about saving that data into a file. I have been doing some research to some...
2
2150
by: =?Utf-8?B?bWFydGluMQ==?= | last post by:
Hi, All, My app picks ccurrent time on the PC to retrieve data from sql DB, the Sql data is always data with non-daylight saving. The app runs on PC with both daylight saving and non-daylight saving. So the question is how to check the PC is daylight saving or not? can anyone know this piece code to handle this problem? Thanks in advance, Martin
1
1700
by: WebCM | last post by:
We can do it in different ways. Configuration data are sent by POST. Some of them should be parsed before saving to file. Perhaps, I will use var_export(). However, how to do it best? 1. Without a class / function. Fields names have no prefix and are equal to $cfg array's indexes. <input name="option_name" /> In every file of options form (e.g. main options, content options, e-
1
5850
by: MSwanston | last post by:
Hi I need some help with saving retreiving data from the cache, and how best to structure my code. FYI am working in VS2005/under .NET2 Framework. Ok, we have a series of reports that get run via a report filter screen, and whilst each report is being generated, it populates a memorystream/streamwriter. This has always been done this way but I am trying to update/improve and generally speed things up. The variables are declared as follows:...
0
947
by: madhu raju | last post by:
iam saving the data into Ms-Access database using Vb.net Application In that application iam using Data grid view to save the data into the database Here in the below code iam saving the name of stalls into the database i have to save according to number given in the textbox .but first i saved only two stallnames and next time i want to save the remaining stall names then it is not possible it checks the starting two stallnames and...
0
9846
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10277
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9413
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7007
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5674
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5853
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4052
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3126
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.