473,386 Members | 1,758 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,386 software developers and data experts.

unable to read char * strings from a buffer

hello everyone, i have a bit of problem reading char * strings from a
buffer (a shared memory, pointed to by 'file_memory').
basically i have a structure in memory 'ShMem' that can be accessed by
2 applications (or will be at least when it is done).
the structure is declared in the procedure under the pointer infoVar.
members tXXL are integer lengths of the strings that as all saved
(concatenated) at address of oAndS_str, which is of type char.
i do not get the right values back however, and i wonder if the tXX
'reconstruction strings' are assigned values properly with memcpy.

void readingTheShMem()
{
int tOTL=0;
int tONL=0;
int tSNL=0;
int tSLL=0;
int tIDL=0;
int tMDL=0;
int readingLength;

//lock the file for writing
memset (&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
fcntl (fd, F_SETLKW, &lock);

struct ShMem *infoVar=(ShMem*)((char*)file_memory);

tOTL = infoVar->oTL;
tONL = infoVar->oNL;
tSNL = infoVar->sNL;
tSLL = infoVar->sLL;
tIDL = infoVar->iDL;
tMDL = infoVar->mDL;
readingLength = tOTL + tONL + tSNL + tSLL + tIDL + tMDL ;
char *readingStrs[readingLength];
printf("reading length = %d\n", readingLength);
printf("the infoVar->oAndS_str reading points
to=%p\n",&(infoVar->oAndS_str));
memcpy(readingStrs, &(infoVar->oAndS_str),
readingLength);

//unlock the file to allow access
lock.l_type = F_UNLCK;
fcntl (fd, F_SETLKW, &lock);

char *tOT[tOTL]; memcpy(tOT,readingStrs,tOTL);
printf("tOTL = %d and oT ptr = %p\n", tOTL, readingStrs);
char *tON[tONL]; memcpy(tON,(readingStrs+tOTL),tONL);
printf("tONL = %d and oN ptr = %p\n", tONL,
(readingStrs+tOTL));
char *tSN[tSNL];
memcpy(tSN,(readingStrs+tOTL+tONL),tSNL);
printf("tSNL = %d and sN ptr = %p\n", tSNL,
(readingStrs+tOTL+tONL));
char *tSL[tSLL];
memcpy(tSL,(readingStrs+tOTL+tONL+tSNL),tSLL);
printf("tSLL = %d and sL ptr = %p\n", tSLL,
(readingStrs+tOTL+tONL+tSNL));
char *tID[tIDL];
memcpy(tID,(readingStrs+tOTL+tONL+tSNL+tSLL),tIDL) ;
printf("tIDL = %d and iD ptr = %p\n", tIDL,
(readingStrs+tOTL+tONL+tSNL+tSLL));
char tMD[tMDL];
memcpy(tMD,(readingStrs+tOTL+tONL+tSNL+tSLL+tIDL), tMDL);
printf("tMDL = %d and mD ptr = %p\n", tMDL,
(readingStrs+tOTL+tONL+tSNL+tSLL+tIDL));

printf("oT = %s\n",tOT);
printf("oN = %s\n",tON);
printf("sN = %s\n",tSN);
printf("sL = %s\n",tSL);
printf("iD = %s\n",tID);
printf("mD = %s\n",tMD);

//do smth with these variables now

}

any ideas where my c++ knowledge is slipping?
thank you for your help
nass

Nov 14 '06 #1
4 3555
nass wrote:
hello everyone, i have a bit of problem reading char * strings from a
buffer (a shared memory, pointed to by 'file_memory').
basically i have a structure in memory 'ShMem' that can be accessed by
2 applications (or will be at least when it is done).
the structure is declared in the procedure under the pointer infoVar.
members tXXL are integer lengths of the strings that as all saved
(concatenated) at address of oAndS_str, which is of type char.
i do not get the right values back however, and i wonder if the tXX
'reconstruction strings' are assigned values properly with memcpy.

void readingTheShMem()
{
int tOTL=0;
int tONL=0;
int tSNL=0;
int tSLL=0;
int tIDL=0;
int tMDL=0;
int readingLength;

//lock the file for writing
memset (&lock, 0, sizeof(lock));
I hope lock is a POD type
(http://www.parashift.com/c++-faq-lit...html#faq-26.7),
or else you're playing with fire when using memset.
lock.l_type = F_WRLCK;
fcntl (fd, F_SETLKW, &lock);

struct ShMem *infoVar=(ShMem*)((char*)file_memory);

tOTL = infoVar->oTL;
tONL = infoVar->oNL;
tSNL = infoVar->sNL;
tSLL = infoVar->sLL;
tIDL = infoVar->iDL;
tMDL = infoVar->mDL;
readingLength = tOTL + tONL + tSNL + tSLL + tIDL + tMDL ;
char *readingStrs[readingLength];
This is not valid C++. The array length must be constant.
printf("reading length = %d\n", readingLength);
printf("the infoVar->oAndS_str reading points
to=%p\n",&(infoVar->oAndS_str));
memcpy(readingStrs, &(infoVar->oAndS_str),
readingLength);
I'm not sure what you're trying to do here, but it looks mighty
suspicious. readingStrs is an array of *pointers*. Did you mean for it
to be a string of characters or a two dimensional array of characters
(i.e., an array of character strings)?
>
//unlock the file to allow access
lock.l_type = F_UNLCK;
fcntl (fd, F_SETLKW, &lock);

char *tOT[tOTL]; memcpy(tOT,readingStrs,tOTL);
printf("tOTL = %d and oT ptr = %p\n", tOTL, readingStrs);
char *tON[tONL]; memcpy(tON,(readingStrs+tOTL),tONL);
printf("tONL = %d and oN ptr = %p\n", tONL,
(readingStrs+tOTL));
char *tSN[tSNL];
memcpy(tSN,(readingStrs+tOTL+tONL),tSNL);
printf("tSNL = %d and sN ptr = %p\n", tSNL,
(readingStrs+tOTL+tONL));
char *tSL[tSLL];
memcpy(tSL,(readingStrs+tOTL+tONL+tSNL),tSLL);
printf("tSLL = %d and sL ptr = %p\n", tSLL,
(readingStrs+tOTL+tONL+tSNL));
char *tID[tIDL];
memcpy(tID,(readingStrs+tOTL+tONL+tSNL+tSLL),tIDL) ;
printf("tIDL = %d and iD ptr = %p\n", tIDL,
(readingStrs+tOTL+tONL+tSNL+tSLL));
char tMD[tMDL];
memcpy(tMD,(readingStrs+tOTL+tONL+tSNL+tSLL+tIDL), tMDL);
printf("tMDL = %d and mD ptr = %p\n", tMDL,
(readingStrs+tOTL+tONL+tSNL+tSLL+tIDL));

printf("oT = %s\n",tOT);
printf("oN = %s\n",tON);
printf("sN = %s\n",tSN);
printf("sL = %s\n",tSL);
printf("iD = %s\n",tID);
printf("mD = %s\n",tMD);

//do smth with these variables now

}

any ideas where my c++ knowledge is slipping?
Use std::string rather than manual character arrays when you can
(http://www.parashift.com/c++-faq-lit...html#faq-34.1). In
this case, you'll probably want to store the data in raw arrays in the
shared mem but copy it into local std::strings.

Cheers! --M

Nov 14 '06 #2
hello and thank you for your help.
using std::string is not an option since these char * values i want
later to assign to a QString class (trolltech's Qt library to
manipulate strings unicode utf-16 bits). and the QString construct can
accept const char * but not string& ...

however, lets stick to the fact i did manage to retrieve the data from
the buffer.. i.e. with printf i can see the expected output on the
console. the thing is in the code i dont take into account anywhere
(among the chars tOT, tON, tSN, tSL, tID and tMD that is) for a NULL
terminator.. or does it do it automatically?

cause now i have problem with the transition from const char * to
QString and i wonder if it could be that the QString constructor does
not find the NULL terminators.
so i m asking if u think that the \0 character is there in my code the
way i have written it or not..
thank you for your help
nass

mlimber wrote:
nass wrote:
hello everyone, i have a bit of problem reading char * strings from a
buffer (a shared memory, pointed to by 'file_memory').
basically i have a structure in memory 'ShMem' that can be accessed by
2 applications (or will be at least when it is done).
the structure is declared in the procedure under the pointer infoVar.
members tXXL are integer lengths of the strings that as all saved
(concatenated) at address of oAndS_str, which is of type char.
i do not get the right values back however, and i wonder if the tXX
'reconstruction strings' are assigned values properly with memcpy.

void readingTheShMem()
{
int tOTL=0;
int tONL=0;
int tSNL=0;
int tSLL=0;
int tIDL=0;
int tMDL=0;
int readingLength;

//lock the file for writing
memset (&lock, 0, sizeof(lock));

I hope lock is a POD type
(http://www.parashift.com/c++-faq-lit...html#faq-26.7),
or else you're playing with fire when using memset.
lock.l_type = F_WRLCK;
fcntl (fd, F_SETLKW, &lock);

struct ShMem *infoVar=(ShMem*)((char*)file_memory);

tOTL = infoVar->oTL;
tONL = infoVar->oNL;
tSNL = infoVar->sNL;
tSLL = infoVar->sLL;
tIDL = infoVar->iDL;
tMDL = infoVar->mDL;
readingLength = tOTL + tONL + tSNL + tSLL + tIDL + tMDL ;
char *readingStrs[readingLength];

This is not valid C++. The array length must be constant.
printf("reading length = %d\n", readingLength);
printf("the infoVar->oAndS_str reading points
to=%p\n",&(infoVar->oAndS_str));
memcpy(readingStrs, &(infoVar->oAndS_str),
readingLength);

I'm not sure what you're trying to do here, but it looks mighty
suspicious. readingStrs is an array of *pointers*. Did you mean for it
to be a string of characters or a two dimensional array of characters
(i.e., an array of character strings)?

//unlock the file to allow access
lock.l_type = F_UNLCK;
fcntl (fd, F_SETLKW, &lock);

char *tOT[tOTL]; memcpy(tOT,readingStrs,tOTL);
printf("tOTL = %d and oT ptr = %p\n", tOTL, readingStrs);
char *tON[tONL]; memcpy(tON,(readingStrs+tOTL),tONL);
printf("tONL = %d and oN ptr = %p\n", tONL,
(readingStrs+tOTL));
char *tSN[tSNL];
memcpy(tSN,(readingStrs+tOTL+tONL),tSNL);
printf("tSNL = %d and sN ptr = %p\n", tSNL,
(readingStrs+tOTL+tONL));
char *tSL[tSLL];
memcpy(tSL,(readingStrs+tOTL+tONL+tSNL),tSLL);
printf("tSLL = %d and sL ptr = %p\n", tSLL,
(readingStrs+tOTL+tONL+tSNL));
char *tID[tIDL];
memcpy(tID,(readingStrs+tOTL+tONL+tSNL+tSLL),tIDL) ;
printf("tIDL = %d and iD ptr = %p\n", tIDL,
(readingStrs+tOTL+tONL+tSNL+tSLL));
char tMD[tMDL];
memcpy(tMD,(readingStrs+tOTL+tONL+tSNL+tSLL+tIDL), tMDL);
printf("tMDL = %d and mD ptr = %p\n", tMDL,
(readingStrs+tOTL+tONL+tSNL+tSLL+tIDL));

printf("oT = %s\n",tOT);
printf("oN = %s\n",tON);
printf("sN = %s\n",tSN);
printf("sL = %s\n",tSL);
printf("iD = %s\n",tID);
printf("mD = %s\n",tMD);

//do smth with these variables now

}

any ideas where my c++ knowledge is slipping?

Use std::string rather than manual character arrays when you can
(http://www.parashift.com/c++-faq-lit...html#faq-34.1). In
this case, you'll probably want to store the data in raw arrays in the
shared mem but copy it into local std::strings.

Cheers! --M
Nov 14 '06 #3
* nass:
[top-posting and overquoting].
Please use some time to get familiar with the conventions in this group.

TIA.,

Alf, The Network Police.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '06 #4

"nass" <at**************@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
hello and thank you for your help.
using std::string is not an option since these char * values i want
later to assign to a QString class (trolltech's Qt library to
manipulate strings unicode utf-16 bits). and the QString construct can
accept const char * but not string& ...
The std::string class has a member function c_str() which will give you what
you need.
however, lets stick to the fact i did manage to retrieve the data from
the buffer.. i.e. with printf i can see the expected output on the
console. the thing is in the code i dont take into account anywhere
(among the chars tOT, tON, tSN, tSL, tID and tMD that is) for a NULL
terminator.. or does it do it automatically?

cause now i have problem with the transition from const char * to
QString and i wonder if it could be that the QString constructor does
not find the NULL terminators.
so i m asking if u think that the \0 character is there in my code the
way i have written it or not..
thank you for your help
nass
Did you fix the problems with the code that you posted earlier? It's simply
not legal to declare an array with a variable as the array size (even though
some compilers might allow it).

If you want an array of char, then declare an array of char with constant
size, like this:
char a[CONST_SIZE]; // (where CONST_SIZE is a compile-time constant)

or, dynamically allocate it like this:
char* pA = new char[non_const_size];

or better, use std::string.

When you declare an array like this:
char* a[CONST_SIZE];
you're specifying an array of char* pointers, NOT an array of char!

And doing this:
char* a[non_const_size];
is simply illegal. (And if your compiler allows it, then it STILL declares
an array of pointers, not an array of char.)

Regarding NULL-terminators for C-style strings: using memcpy will only copy
a NULL-terminator if there is already one in the source (and it doesn't
matter where in the data that 0 is, it will copy the amount specified,
regardless of the presence of one or more 0s).

You need to add a NULL-terminator yourself if you use memcpy. (And you need
to remember to allocate room for that NULL-terminator in your destination
array!)

Using strncpy is also a possible solution. (But again, std::string is
better.)

-Howard
Nov 14 '06 #5

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

Similar topics

0
by: sangui | last post by:
Helllo. this is biginner programmer. Would u check the file that I programed on c#(winform)? I tryed to make the program reading the binary file by C# programming but I failed. If u have...
0
by: sangui | last post by:
Hello. this is beginner programmer. If u have more time, would you check this code.? please give me the answer how to slove this problem...? ÷ºÎ ÆÄÀÏ ==> wtmpx.dat ·Î±× ÆÄÀÏ ...
0
by: Aryeh Holzer | last post by:
Hi, I've been trying to use the weather webservice available from the National Weather Service (NWS), at http://www.nws.noaa.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML. wsdl without success. Here's...
0
by: quocvuong2005 | last post by:
Hi all ! I have a webservice that contains a Helloworld() method (This return a string type ) and a asp.net application . I call Helloworld method from webservice , it works fine . but when i call...
15
by: I. Myself | last post by:
This is about reading the .ini files which are used in several of our projects. Currently, the actual read of each line of the file is with this statement: fscanf(file, "%s %s", name, value);...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
14
by: WStoreyII | last post by:
the following code is supposed to read a whole line upto a new line char from a file. however it does not work. it is producing weird results. please help. I had error checking in there for...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.