473,699 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(v oid *pData,DATA enData)
{
void * pTempStructArra y;
if (enData == STUDENT)
{
pTempStructArra y= new student[20];
iSizeOfStruct = sizeof(student) ;
}
else
{
pTempStructArra y= new staff[20];
iSizeOfStruct = sizeof(staff);
}

ReadFile(hFile, &pTempStructArr ay [0], iSizeOfStruct,
&NumBytesReadWr itten, 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 2360
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(v oid *pData,DATA enData)
{
void * pTempStructArra y;
if (enData == STUDENT)
{
pTempStructArra y= new student[20];
iSizeOfStruct = sizeof(student) ;
}
else
{
pTempStructArra y= new staff[20];
iSizeOfStruct = sizeof(staff);
}

ReadFile(hFile, &pTempStructArr ay [0], iSizeOfStruct,
&NumBytesReadWr itten, 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
"&pTempStructAr ray [0]"
of the statement
ReadFile(hFile, &pTempStructArr ay [0], ...

If examine this parameter really closely you'll find that pTempStructArra y[0] is
the trouble-maker: You want to access the first element of this array. As
pTempStructArra y 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
pTempStructArra y[0] is in this case the same as pTempStructArra y + 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, pTempStructArra y, ...
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************ **********@t8g2 000prg.googlegr oups.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(v oid *pData,DATA enData)
{
void * pTempStructArra y;
if (enData == STUDENT)
{
pTempStructArra y= new student[20];
iSizeOfStruct = sizeof(student) ;
}
else
{
pTempStructArra y= new staff[20];
iSizeOfStruct = sizeof(staff);
}

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

void ReadWriteStuden tData(...);
void ReadWriteStaffD ata(...);

So a generalized solution can be composed using the above:

void ReadWriteData(D ata enData)
{
if (enData == STUDENT)
return ReadWriteStuden tData(...);

if (enData == STAFF)
return ReadWriteStaffD ata(...);
}
Nov 6 '07 #5
On Nov 6, 4:07 pm, santosh <santosh....@gm ail.comwrote:
On Tuesday 06 Nov 2007 4:16 pm Nash <jeevs...@gmail .comwrote in
article <1194346007.888 667.116...@t8g2 000prg.googlegr oups.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 misunderstandin g there: The compiler won't disallow you to
read the contents of the file using a void* pointer. It disallows to use the
expression &pTempStructArr ay [0]. Replace the following line

ReadFile(hFile, &pTempStructArr ay [0], iSizeOfStruct,
&NumBytesReadWr itten, NULL);

by

ReadFile(hFile, pTempStructArra y, iSizeOfStruct,
&NumBytesReadWr itten, 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 misunderstandin g there: The compiler won't disallow you to
read the contents of the file using a void* pointer. It disallows to use the
expression &pTempStructArr ay [0]. Replace the following line

ReadFile(hFile, &pTempStructArr ay [0], iSizeOfStruct,
&NumBytesReadWr itten, NULL);

by

ReadFile(hFile, pTempStructArra y, iSizeOfStruct,
&NumBytesReadWr itten, 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
4963
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 webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
4
3622
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 useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
9
3740
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 execute this function dynamically. So I have a function list in database written as a string. I am now looking for function or mechanism which will execute function dynamically. I am here Giving a example.
23
7809
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 called, can be a genuine no-op. Consider: typedef int(*polymorphic_func)(int param);
6
2094
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 > struct some_struct structure
3
3553
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 it into my own user defined structures. Since the process of opening and retrieving data from the database is exactly the same for all struct types, and the only part that's different is how the data is copied into the diff structs, I was...
28
4700
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
376
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
2224
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 think my way through what I want to do now. I don't know why - I'm fine with most other aspects of the language, but my brain goes numb when I'm reading about function pointers! I would like to have an array of structures, something like
0
8685
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...
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8905
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
8880
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
7743
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...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
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();...
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.