Transparent accessing structure members in a loop

M

Madhur

Hello,
Suppose I have a structure like this:

struct LOGFILE
{
char timeStamp[100];
char Message1[100];
char Message2[100];
char Message3[100];
char Message4[100];
char Message5[100];


};

I have a memory mapped file (baiscally) and I want to read the file and
assign the values to this structure. Since the file is unstructured in
memory , I will parse and look for tab breaks and assign values accordingly.

Now, I want to do this in a loop. For ex:
LOGFILE *logFile; //Assume points to valid structure
void *fileData; //Pointer to file in memory (beginning)

for (int i=0;i<fileSizeByte;i++)
{


if(data=='\t')
{
memcpy(spLog->timeStamp,fileData,i);
}

}

Now , as the next text is found after tab, I want to copy it in message1,
then message 2 and so on. And then repeat copying to the new structure
pointer. Is it possible to do it transparently, without explicitly
duplicating the code for each structure member ?

Thanks,
Madhur
 
A

Andrea Crotti

Madhur said:
Hello,
Suppose I have a structure like this:

struct LOGFILE
{
char timeStamp[100];
char Message1[100];
char Message2[100];
char Message3[100];
char Message4[100];
char Message5[100];


};

I have a memory mapped file (baiscally) and I want to read the file
and assign the values to this structure. Since the file is
unstructured in memory , I will parse and look for tab breaks and
assign values accordingly.

Now, I want to do this in a loop. For ex:
LOGFILE *logFile; //Assume points to valid structure
void *fileData; //Pointer to file in memory (beginning)

for (int i=0;i<fileSizeByte;i++)
{


if(data=='\t')
{
memcpy(spLog->timeStamp,fileData,i);
}

}

Now , as the next text is found after tab, I want to copy it in
message1, then message 2 and so on. And then repeat copying to the new
structure pointer. Is it possible to do it transparently, without
explicitly duplicating the code for each structure member ?

Thanks,
Madhur

Well in C++ I don't think you should use char[] and memcpy almost at
all.
I have something which reads line and store them and I use something like

std::ifstream ifs(config_file, std::ifstream::in);

char buf[MAX_LINE_LENGTH];
while (ifs.good()) {
ifs.getline(buf, MAX_LINE_LENGTH);
std::string b = buf;


the char[] is only temporary, then I only use strings which have the
right size.
Maybe it can be done even better.
Then you should maybe do something like
std::map<string, string>

to store and iterate over your data...
 
R

RaZiel

Hello,
Suppose I have a structure like this:

struct LOGFILE
{
char timeStamp[100];
char Message1[100];
char Message2[100];
char Message3[100];
char Message4[100];
char Message5[100];


};

I have a memory mapped file (baiscally) and I want to read the file and
assign the values to this structure. Since the file is unstructured in
memory , I will parse and look for tab breaks and assign values
accordingly.

Now, I want to do this in a loop. For ex:
LOGFILE *logFile; //Assume points to valid structure
void *fileData; //Pointer to file in memory (beginning)

for (int i=0;i<fileSizeByte;i++)
{


if(data=='\t')
{
memcpy(spLog->timeStamp,fileData,i);
}

}

Now , as the next text is found after tab, I want to copy it in
message1, then message 2 and so on. And then repeat copying to the new
structure pointer. Is it possible to do it transparently, without
explicitly duplicating the code for each structure member ?

Thanks,
Madhur

Yes. LOGFILE is a POD type. You just write and read with the size of
LOGFILE. Add the size of the delimiter when doing memcpy, but I can't
see why you need a delimiter when the size of LOGFILE is already known.

- RaZ
 
R

RaZiel

Madhur said:
Hello,
Suppose I have a structure like this:

struct LOGFILE
{
char timeStamp[100];
char Message1[100];
char Message2[100];
char Message3[100];
char Message4[100];
char Message5[100];


};

I have a memory mapped file (baiscally) and I want to read the file
and assign the values to this structure. Since the file is
unstructured in memory , I will parse and look for tab breaks and
assign values accordingly.

Now, I want to do this in a loop. For ex:
LOGFILE *logFile; //Assume points to valid structure
void *fileData; //Pointer to file in memory (beginning)

for (int i=0;i<fileSizeByte;i++)
{


if(data=='\t')
{
memcpy(spLog->timeStamp,fileData,i);
}

}

Now , as the next text is found after tab, I want to copy it in
message1, then message 2 and so on. And then repeat copying to the new
structure pointer. Is it possible to do it transparently, without
explicitly duplicating the code for each structure member ?

Thanks,
Madhur

Well in C++ I don't think you should use char[] and memcpy almost at
all.

Why? Doesn't make sense at all.
I have something which reads line and store them and I use something like

std::ifstream ifs(config_file, std::ifstream::in);

char buf[MAX_LINE_LENGTH];
while (ifs.good()) {
ifs.getline(buf, MAX_LINE_LENGTH);
std::string b = buf;

You could actually avoid using char buf[] here, and with the possibility
of easy error handling.

std::string line;
while (std::getline(file, line))
{
std::string b = line.substr(0, MAX_LINE_LENGTH);
....
the char[] is only temporary, then I only use strings which have the
right size.
Maybe it can be done even better.
Then you should maybe do something like
std::map<string, string>
Why? Mapping what to what?
to store and iterate over your data...

- RaZ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top