473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Generalize function to read different structure from file

Hi,
I have two different files which is filled with data from two
different structures like

struct student
{
char* name;
int age;
};

which is stored in student.dat file and another structure

struct staff
{
char *name,
int age;
int exp;
};

which is stored in staff.dat file. I have to write a function which
should read the first 20 data and write it into sepearate file. I
thought of generalizing the function so i had a function like
enum DATA
{
STUDENT,
STAFF
};
ReadWriteData(void *pData,DATA enData)
{
void * pTempStructArray;
if (enData == STUDENT)
{
pTempStructArray= new student[20];
iSizeOfStruct = sizeof(student);
}
else
{
pTempStructArray= new staff[20];
iSizeOfStruct = sizeof(staff);
}

ReadFile(hFile, &pTempStructArray [0], iSizeOfStruct,
&NumBytesReadWritten, NULL);
}

but the problem is if i try to read the data into this void pointer it
is giving some error like "error C2036: 'void *' : unknown size". Any
idea to generalize this function so that i can used the same function
to copy both the data.

Nov 6 '07 #1
7 2334
Nash wrote:
Hi,
I have two different files which is filled with data from two
different structures like

struct student
{
char* name;
int age;
};

which is stored in student.dat file and another structure

struct staff
{
char *name,
int age;
int exp;
};

which is stored in staff.dat file. I have to write a function which
should read the first 20 data and write it into sepearate file. I
thought of generalizing the function so i had a function like
enum DATA
{
STUDENT,
STAFF
};
ReadWriteData(void *pData,DATA enData)
{
void * pTempStructArray;
if (enData == STUDENT)
{
pTempStructArray= new student[20];
iSizeOfStruct = sizeof(student);
}
else
{
pTempStructArray= new staff[20];
iSizeOfStruct = sizeof(staff);
}

ReadFile(hFile, &pTempStructArray [0], iSizeOfStruct,
&NumBytesReadWritten, NULL);
}

but the problem is if i try to read the data into this void pointer it
is giving some error like "error C2036: 'void *' : unknown size". Any
idea to generalize this function so that i can used the same function
to copy both the data.
The problems of this piece of code are so many that its hard to find a place to
start with. So lets just analyze what you have coded so far:
The compiler complains that it cannot evaluate the expression
"&pTempStructArray [0]"
of the statement
ReadFile(hFile, &pTempStructArray [0], ...

If examine this parameter really closely you'll find that pTempStructArray[0] is
the trouble-maker: You want to access the first element of this array. As
pTempStructArray is defined as void*, the compiler doesn't know the type of the
pointer (it knows that it is a pointer, but it doesn't know anything about the
data that is pointed to).

Thus you cannot use any pointer arithmetic on this pointer (bear in mind that
pTempStructArray[0] is in this case the same as pTempStructArray + 0). The
reason for this is that if you have a pointer "p" to some type "T", the
expression "p[number]" denotes the memory location &p plus number times the size
of T in bytes. In your case you give the compiler no information about the type
pointed to (the compiler cannot take the size of void).

You could argue that the first element of an array has the same address as the
array itself, so the compiler wouldn't need to know the size of the type pointed
to in this particular case. Although the compiler _could_ generate proper code
for this, the C++ standard certainly allows it to issue an error.

To get your code compiled you can simply pass the address of the array in the
call to ReadFile:
ReadFile(hFile, pTempStructArray, ...
will do just fine.

Having done this, you'll quickly find out about the other errors of your
programme, but this is a story for a different posting.

Regards,
Stuart
Nov 6 '07 #2
Hi Stuart,
Thanks for your quick response. The crux of the posting is to get
different ideas for implementing the solution to the problem. If the
compiler will not allow me to read the file using void * thats ok. Is
there any way to actually implement this functionality??

Thanks in advance.

Nov 6 '07 #3
On Tuesday 06 Nov 2007 4:16 pm Nash <je******@gmail.comwrote in
article <11**********************@t8g2000prg.googlegroups. com>:
Hi Stuart,
Thanks for your quick response.
Please quote the relevant portions of the message to which you're
replying. There are times when previous articles are not available on
an Usenet server, for whatever reason.
The crux of the posting is to get
different ideas for implementing the solution to the problem. If the
compiler will not allow me to read the file using void * thats ok. Is
there any way to actually implement this functionality??
At some point in the program there has to be some separate logic for
each type of data. If needed you can abstract the function that reads
the files containing the struct objects into memory. However when you
need to perform some operation on the data you need to know the type.

Generic I/O functions like fread() and fwrite() can read any type of
data. You can write functions to call them in an appropriate manner to
read the different types of data. Or you can read the data to an array
of unsigned char. But in the latter case you need to know the type if
you want to perform most operations on the data portably.
Nov 6 '07 #4
[snip]
>
which is stored in staff.dat file. I have to write a function which
should read the first 20 data and write it into sepearate file. I
thought of generalizing the function so i had a function like
enum DATA
{
STUDENT,
STAFF
};
ReadWriteData(void *pData,DATA enData)
{
void * pTempStructArray;
if (enData == STUDENT)
{
pTempStructArray= new student[20];
iSizeOfStruct = sizeof(student);
}
else
{
pTempStructArray= new staff[20];
iSizeOfStruct = sizeof(staff);
}

ReadFile(hFile, &pTempStructArray [0], iSizeOfStruct,
&NumBytesReadWritten, NULL);
}
Generalization often comes after specialization. That is, before you
start to write the above generalized function, you may consider writing:

void ReadWriteStudentData(...);
void ReadWriteStaffData(...);

So a generalized solution can be composed using the above:

void ReadWriteData(Data enData)
{
if (enData == STUDENT)
return ReadWriteStudentData(...);

if (enData == STAFF)
return ReadWriteStaffData(...);
}
Nov 6 '07 #5
On Nov 6, 4:07 pm, santosh <santosh....@gmail.comwrote:
On Tuesday 06 Nov 2007 4:16 pm Nash <jeevs...@gmail.comwrote in
article <1194346007.888667.116...@t8g2000prg.googlegroups. com>:
Hi Stuart,
Thanks for your quick response.

Please quote the relevant portions of the message to which you're
replying. There are times when previous articles are not available on
an Usenet server, for whatever reason.
The crux of the posting is to get
different ideas for implementing the solution to the problem. If the
compiler will not allow me to read the file using void * thats ok. Is
there any way to actually implement this functionality??

At some point in the program there has to be some separate logic for
each type of data. If needed you can abstract the function that reads
the files containing the struct objects into memory. However when you
need to perform some operation on the data you need to know the type.

Generic I/O functions like fread() and fwrite() can read any type of
data. You can write functions to call them in an appropriate manner to
read the different types of data. Or you can read the data to an array
of unsigned char. But in the latter case you need to know the type if
you want to perform most operations on the data portably.
thanks santhosh for your pointers.

Nov 6 '07 #6
Nash wrote:
Hi Stuart,
Thanks for your quick response. The crux of the posting is to get
different ideas for implementing the solution to the problem. If the
compiler will not allow me to read the file using void * thats ok. Is
there any way to actually implement this functionality??
There may a bit of an misunderstanding there: The compiler won't disallow you to
read the contents of the file using a void* pointer. It disallows to use the
expression &pTempStructArray [0]. Replace the following line

ReadFile(hFile, &pTempStructArray [0], iSizeOfStruct,
&NumBytesReadWritten, NULL);

by

ReadFile(hFile, pTempStructArray, iSizeOfStruct,
&NumBytesReadWritten, NULL);

and the compiler will happily compile it (if you fix the other syntactical
errors in the code you have posted). Keep in mind that the programme contains
several semantical errors (the code does not do what you think it is doing).
Before you get to debug these, you'll have to get the code compiled, though.

Regards,
Stuart
Nov 6 '07 #7
On Nov 6, 5:17 pm, Stuart Redmann <DerTop...@web.dewrote:
Nash wrote:
Hi Stuart,
Thanks for your quick response. The crux of the posting is to get
different ideas for implementing the solution to the problem. If the
compiler will not allow me to read the file using void * thats ok. Is
there any way to actually implement this functionality??

There may a bit of an misunderstanding there: The compiler won't disallow you to
read the contents of the file using a void* pointer. It disallows to use the
expression &pTempStructArray [0]. Replace the following line

ReadFile(hFile, &pTempStructArray [0], iSizeOfStruct,
&NumBytesReadWritten, NULL);

by

ReadFile(hFile, pTempStructArray, iSizeOfStruct,
&NumBytesReadWritten, NULL);

and the compiler will happily compile it (if you fix the other syntactical
errors in the code you have posted). Keep in mind that the programme contains
several semantical errors (the code does not do what you think it is doing).
Before you get to debug these, you'll have to get the code compiled, though.

Regards,
Stuart
thanks stuart for your help. i know that my code will give compilation
error i will fix it and let you know whether it is working.

Nov 12 '07 #8

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
9
by: Kishor | last post by:
Hi all, I am Using VB.Net for developing my application. I am now needed help. In this project I have to execute some function, but I cannot call them directly using function name, I wanted to...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
6
by: dndfan | last post by:
Hello, In the short time I have spent reading this newsgroup, I have seen this sort of declaration a few times: > int > func (string, number, structure) > char* string > int number >...
3
by: markww | last post by:
Hi, I have a wrapper around some 3rd party database library function. The pseudo code looks like the following - it is meant to open a table in a database, extract values from a table, then copy...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
6
by: Nash | last post by:
Hi, I have two different files which is filled with data from two different structures like struct student { char* name; int age; };
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.