PDA

View Full Version : unable to save QCStrings properly in a buffer



nass
13th November 2006, 17:04
hello everyone,
i am having some bit of trouble while trying to save on a shared memory segment.. just a bufer really for all interest here. i get a segmentation fault when i try to save some QCStrings concatenated together using memcpy.. please have a look.



void monitorForm::int()
{
stationNum="1234";
stationLoc="hello";
operatorName="hihi";
operatorTel="12345678";
installationDate="10/10/2000";
maintenanceDate="22/22/2000";
saveInfo(operatorTel,operatorName,stationNum,stati onLoc,installationDate,maintenanceDate);
}

/*.....*/

void monitorForm::saveInfo(QString tempOperTel, QString tempOperName, QString tempStatNum, QString tempStatLoc,
QString tempInstallDate, QString tempMaintenDate)
{
//convert to ASCII
QCString tOT = tempOperTel.local8Bit();
QCString tON = tempOperName.local8Bit();
QCString tSN = tempStatNum.local8Bit();
QCString tSL = tempStatLoc.local8Bit();
QCString tID = tempInstallDate.local8Bit();
QCString tMD = tempMaintenDate.local8Bit();

int tOTL = tOT.length();
int tONL = tON.length();
int tSNL = tSN.length();
int tSLL = tSL.length();
int tIDL = tID.length();
int tMDL = tMD.length();

const char *savingStrs=tOT+tON+tSN+tSL+tID+tMD;
int savingLength = tOTL + tONL + tSNL + tSLL + tIDL + tMDL ;

printf("savingStrs = %s\n",savingStrs);
printf("%d+%d+%d+%d+%d+%d = %d\n",tOTL , tONL , tSNL , tSLL , tIDL , tMDL , savingLength);

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

//file_memory is a pointer to the zero offset of the sh.mem. segment
//opcode offset is just an offset in the sh.mem where we write some index values
*((char*)file_memory+wOpcodeOffset) = 9;
struct ShMem *infoVar=(ShMem*)((char*)file_memory);

infoVar->oTL = tOTL;
infoVar->oNL = tONL;
infoVar->sNL = tSNL;
infoVar->sLL = tSLL;
infoVar->iDL = tIDL;
infoVar->mDL = tMDL;
printf("hello\n");
memcpy(infoVar->oAndS_str, savingStrs, savingLength);
printf("hello2\n");

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

the code crashes on memcpy(...). that is between printf("hello\n"); and printf("hello2\n");

i have noticed something awkard. when i printf savingStrs i get exactly the following output:
savingStrs =5678hihi1234hello10/10/200022/22/2000

but the savingLength variable returns 41 which is correct and expected.

(notice that the debugging output doesn't have a space " " after the equals sign "=". but especially notice that 4 first bytes of the variable tempOperTel has been trunsated) any ideas?
thank you for your help
nass

jacek
13th November 2006, 17:14
const char *savingStrs=tOT+tON+tSN+tSL+tID+tMD;
You create a temporary QCString object, obtain a pointer to its internal data and then that object gets destroyed.

Try:
QCString tmp = tOT+tON+tSN+tSL+tID+tMD;
const char *savingStrs = tmp;
int savingLength = tmp.length()

Are you sure that infoVar->oAndS_str is a correct pointer?

nass
13th November 2006, 17:25
thank you for your help, we are halfway there.
i did as you said and yes i do get the right output now.

savingStrs = 12345678hihi1234hello10/10/200022/22/2000

however i still get a seg. fault. after printf("hello\n");..

i wonder if it could possibly not like the infoVar->oAndS_str pointer. it is defined as char * in the sh.mem. structure.

what did you mean if itsthe correct pointer?

jacek
13th November 2006, 17:55
what did you mean if itsthe correct pointer?
For example "char *ptr = 0xdeafbeef" is most likely an invalid pointer.

nass
13th November 2006, 19:08
i still don't see where is the fault...:( im sorry but im just about learning c++...
here is the shared memory structure as defined so as to perhaps help identify if there is a fault

look at the bottom for the defined members of shared structure that are accessed through the declaration infoVar (of type ShMem)


//-------------------------------Shared Memory Structure------------------------ ----------
struct SchedDateTime
{
int cTH;
int cTMin;
int cTD;
int cTMon;
int cTY;
};

struct CSched
{
SchedDateTime On;
SchedDateTime Off;
};

struct ShMem
{
unsigned char opCode0;
unsigned char opCode1;
unsigned char opCode2;
unsigned char opCode3;

float cPow;
float cRPow;
float cVolt;
float cC1C;
float cC2C;
float cC3C;
/*float curCapCurrent[3];*/

unsigned char cC1S;
unsigned char cC2S;
unsigned char cC3S;
/*unsigned char curCapState[3];*/

int appDTOff;

/*unsigned char operationalState;*/
unsigned char operState;
unsigned char logSampPer;
unsigned char logT;
unsigned char chLog;
//-----------------------------powerSensor
unsigned char powerSR;
int powerRes;
int powerMinI;
int powerMaxI;
int powerF;
float powerDCBO;
//-----------------------------ReactiveSensor
unsigned char rPowerSR;
int rPowerRes;
int rPowerMinI;
int rPowerMaxI;
int rPowerF;
float rPowerDCBO;
//-----------------------------VoltageSensor
unsigned char voltageSR;
int voltageRes;
int voltageMinI;
int voltageMaxI;
int voltageF;
float voltageDCBO;
//-----------------------------IC1Sensor
unsigned char cap1SR;
int cap1Res;
int cap1MinI;
int cap1MaxI;
int cap1F;
float cap1DCBO;
//-----------------------------IC2Sensor
unsigned char cap2SR;
int cap2Res;
int cap2MinI;
int cap2MaxI;
int cap2F;
float cap2DCBO;
//-----------------------------IC3Sensor
unsigned char cap3SR;
int cap3Res;
int cap3MinI;
int cap3MaxI;
int cap3F;
float cap3DCBO;
//-----------Auto Mode Config Parameters
float aMon;
float aMoff;
float aKon;
float aKoff;
int aInTime;
//-----------Manual Mode Config Parameters
unsigned char mC1St;
unsigned char mC2St;
unsigned char mC3St;
/*unsigned char MANUAL_capstate[3];*/


//------------Timer Mode Config Parameters
/*unsigned char TIMER_cap1_daily;
unsigned char TIMER_cap2_daily;
unsigned char TIMER_cap3_daily;*/

/*unsigned char Timer_cap1_daily[3];*/
unsigned char c1TD;
unsigned char c2TD;
unsigned char c3TD;
CSched c1Sched;
CSched c2Sched;
CSched c3Sched;
/*GUISchedule cxTimerOnOff[3];*/

/*an alarm code*/
unsigned char alarmCode;

/*autoCalibration signal*/
unsigned char aCc; //the caller (1of6 sensors)

/*synch error correction flag*/
unsigned char synchErr;
unsigned char rPowErr;

/*station and operator infromation*/
int oTL;
int oNL;
int sNL;
int sLL;
int iDL;
int mDL;
char *oAndS_str;

};

thank you for your help
nass

jacek
13th November 2006, 19:25
Where does file_memory comes from? Does it have a correct value? How did you initialize the area that file_memory points to? What is the value of infoVar->oAndS_str? Are there any other programs that use that shared memory?

nass
14th November 2006, 00:03
file_memory is initialised using standard linux init descriptors.. nothing weird here..



void initSharingMem()
{
/* Open the file. */
fd = open (file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (fd==-1) printf("error in opening file");
lseek (fd, FILE_LENGTH+1, SEEK_SET);
write (fd, "", 1);
lseek (fd, 0, SEEK_SET);
/* Create the memory-mapping. */
file_memory = mmap (0, FILE_LENGTH, PROT_READ | PROT_WRITE,MAP_SHARED, fd, 0);
}


file_memory should be right since the saving of the integers is done properly, not to mention that the rest of the variables you see in the sh.mem header file, as saved and retried in a similar manner in other procedures. so i doubt file_memory pointer outputs a wrong value.
it is something in the range 0x8000xxxx .. im not quite sure i can check tomorrow from my work.

well infoVar->oAndS_str is a pointer it should point to the beginning of the characters that make up the string... or rather, the stream of char is moved from the 'head' to the location where infoVar->oAndS_str looks.

no other programs use the shared memory yet, the other code is being developed currently.

jacek
14th November 2006, 00:18
well infoVar->oAndS_str is a pointer it should point to the beginning of the characters that make up the string...
If you can successfully access and change other fields of that structure, then I would double check where does infoVar->oAndS_str point.

On the second thought, how do you expect it to work? That shared memory segment might be mapped into each process's address space in a different place, so each process should use a different pointer. Shouldn't you use a char array or some offset instead?


no other programs use the shared memory yet, the other code is being developed currently.
OK, just don't forget that different compilers can align ShMem fields in a different way.

nass
14th November 2006, 11:58
hm ok i found out the problem.
i checked and infoVar->oAndS_str points to nil. that definitely is not right, but i have to ask why.. i mean it appears that a pointer inside a structure is not even given 8bits on structure declaration in the code ?????!, cause if it is, it should be able to be adressed

So instead i declared oAndS_str as char, so that it can then use its reference (address). it does work correctly now
Thankyou for your help.
nass

nass
14th November 2006, 13:26
aha! so i managed to do thesaving part properly but now i am having trouble with doing the inverse, that is reading from the shared memory the strings.
i thought that the idea was:
read the lengths of each string
memcpy all the string to a char* all_the_strings variable,
and then declare as many char* variables as there are strings (preknown) and
memcpy each_strings_length characters from the all_the_strings variable
to each separate char *...
here is how i mplemented that:



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);

operatorTel = QString::fromLocal8Bit((const char*)tOT);
operatorName = QString::fromLocal8Bit((const char*)tON);
stationNum = QString::fromLocal8Bit((const char*)tSN);
stationLoc = QString::fromLocal8Bit((const char*)tSL);
installationDate = QString::fromLocal8Bit((const char*)tID);
maintenanceDate = QString::fromLocal8Bit((const char*)tMD);

//do smth with these variables now

}


and the output i get


reading length = 41
the infoVar->oAndS_str reading points to = 0xb7736168
tOTL = 8 and oT ptr = 0xbfcfa020
tONL = 4 and oN ptr = 0xbfcfa168
tSNL = 4 and sN ptr = 0xbfcfa20c
tSLL = 5 and sL ptr = 0xbfcfa2b0
tIDL = 10 and iD ptr = 0xbfcfa37d
tMDL = 10 and mD ptr = 0xbfcfa517
ϿПϿ�Ͽ ϿPϿ8hihi1234hello10/10/200022/22/2000;
oN = |
sN =
sL = �h
iD = Ͽ
mD =

do you have any idea where my train of thinking is faulting?
nass

nass
14th November 2006, 15:27
i managed to read the right data from the buffer with printf, by making char readingStrs[readingLength] a proper variable (instead of a char *)..
the funny thing is that in memcpy i am supposed to give pointers to parameters. but if i try to get a reference & of readingStrs, the code fails to execute correctly!!! why is that?

here is the code


void readFromShMem()
{
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 cursStruct *cursVar=(cursStruct*)((char*)file_memory + 0x4);
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);
printf("readingStrs contains after = %s\n",readingStrs);

//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));

//the printf's below output the right values on console
printf("oT = %.8s.\n",tOT);
printf("oN = %.4s.\n",tON);
printf("sN = %.4s.\n",tSN);
printf("sL = %.5s.\n",tSL);
printf("iD = %.10s.\n",tID);
printf("mD = %.9s.\n",tMD);

//but the QString values below do not output the right values when they are 'setText'd' in QLabels.
operatorTel = QString::fromLocal8Bit((const char*)tOT);
operatorName = QString::fromLocal8Bit((const char*)tON);
stationNum = QString::fromLocal8Bit((const char*)tSN);
stationLoc = QString::fromLocal8Bit((const char*)tSL);
installationDate = QString::fromLocal8Bit((const char*)tID);
maintenanceDate = QString::fromLocal8Bit((const char*)tMD);

}

so while i do have the right printfs, i get some reduntant chars on the QLAbels if i try to ->setText() one of the above QStrings in them...
any ideas?

jacek
15th November 2006, 00:04
the funny thing is that in memcpy i am supposed to give pointers to parameters. but if i try to get a reference & of readingStrs, the code fails to execute correctly!!! why is that?
Because you can treat an array of chars just like a pointer to the first character. While &readingStrs is a pointer to an array (something more like char**, but not exactly).


//the printf's below output the right values on console
printf("oT = %.8s.\n",tOT);
...
These printfs work, because you have specified each string length in the format. Probably you forgot about the terminating NUL character.

Luckily QString::fromLocal8Bit() has a second parameter and you can simply do this:


operatorTel = QString::fromLocal8Bit( readingStrs, tOTL );
operatorName = QString::fromLocal8Bit( readingStrs + tOTL, tONL);
...

nass
15th November 2006, 13:19
there is one final (hopefully!) problem i am having however. if i save greek chars on a QFile using streaming operations and re-read them i get ???? (as many ? as there where supposed to be greek chars).

in the beginning i would get crazy QCString lengths (for 3 greek chars i'd get a length of 24 or smth), so i played around with QTextCodec abit and managed to make - at least measuring the length correctly - work. Then i remove from aQLabel->setText() the QString::fromLocal8Bit function and on saving and reading from shared memory the characters appear and work as expected. Here is the code that DOES work for the shared memory. Then i will post the write - read to QFile functions.

here is the saving code for shared memory:
the loc variable is QTextCodec * , declared as private variable in this form and initialized in init() as


loc = QTextCodec::codecForName("ISO8859-7");//extended ASCII with greek
loc->setCodecForCStrings(loc);

then the code is ..


{
QCString tOT = loc->fromUnicode(tempOperTel);
QCString tON = loc->fromUnicode(tempOperName);
QCString tSN = loc->fromUnicode(tempStatNum);
QCString tSL = loc->fromUnicode(tempStatLoc);
QCString tID = loc->fromUnicode(tempInstallDate);
QCString tMD = loc->fromUnicode(tempMaintenDate);

//i have commented the lines below since they didn't show the greek characters
//correctly
/*QCString tOT = tempOperTel.local8Bit();
QCString tON = tempOperName.local8Bit();
QCString tSN = tempStatNum.local8Bit();
QCString tSL = tempStatLoc.local8Bit();
QCString tID = tempInstallDate.local8Bit();
QCString tMD = tempMaintenDate.local8Bit();*/

int tOTL = tOT.length();
int tONL = tON.length();
int tSNL = tSN.length();
int tSLL = tSL.length();
int tIDL = tID.length();
int tMDL = tMD.length();

QCString tmp=tOT+tON+tSN+tSL+tID+tMD;
const char *savingStrs = tmp;
int savingLength = tmp.length() ;

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

*((char*)file_memory+wOpcodeOffset) = 9;
struct ShMem *infoVar=(ShMem*)((char*)file_memory);

infoVar->oTL = tOTL;
infoVar->oNL = tONL;
infoVar->sNL = tSNL;
infoVar->sLL = tSLL;
infoVar->iDL = tIDL;
infoVar->mDL = tMDL;
memcpy(&(infoVar->oAndS_str), savingStrs, savingLength);

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

and then i reuse the QTextCodec to read the data from the shared memory:
the reading code for shared memory is:



{
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];

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+1]; memcpy(tOT,readingStrs,tOTL); tOT[tOTL]='\0';
char tON[tONL+1]; memcpy(tON,(readingStrs+tOTL),tONL); tON[tONL]='\0';
char tSN[tSNL+1]; memcpy(tSN,(readingStrs+tOTL+tONL),tSNL); tSN[tSNL]='\0';
char tSL[tSLL+1]; memcpy(tSL,(readingStrs+tOTL+tONL+tSNL),tSLL); tSL[tSLL]='\0';
char tID[tIDL+1]; memcpy(tID,(readingStrs+tOTL+tONL+tSNL+tSLL),tIDL) ; tID[tIDL]='\0';
char tMD[tMDL+1]; memcpy(tMD,(readingStrs+tOTL+tONL+tSNL+tSLL+tIDL), tMDL); tMD[tMDL]='\0';

operatorTel = loc->toUnicode(tOT);
operatorName = loc->toUnicode(tON);
stationNum = loc->toUnicode(tSN);
stationLoc = loc->toUnicode(tSL);
installationDate = loc->toUnicode(tID);
maintenanceDate = loc->toUnicode(tMD);

//do smth now
}

am i using QTextCodec correctly?

now for saving to File :


void monitorForm::writeToGuiFile()
{
QCString buffer=loc->fromUnicode(stationNum)+"\nEOV\n"+loc->fromUnicode(stationLoc)+"\nEOV\n"+
loc->fromUnicode(operatorName)+"\nEOV\n"+loc->fromUnicode(operatorTel)+"\nEOV\n"+
loc->fromUnicode(installationDate)+"\nEOV\n"+loc->fromUnicode(maintenanceDate)+"\nEOV\n";
QFile file(guiFileName);
if ( file.open( IO_WriteOnly ) )
{
QTextStream stream( &file );
stream<<buffer;
}
file.close();
}

if say QString operatorName is in greek, and i check the text file after the operation, i observe ??? instead of the text. So if i manually erase the ??? and put a greek word back in, and i read the file from my code, the data is shown correctly in the QLabels.
here is the reading QFile function


int monitorForm::readFromGuiFile()
{
QFile file(guiFileName);
QCString lines[6]="";
QCString curLine="";
QCString sumLine="";
int i=0;
if ( file.open( IO_ReadOnly ) )
{
QTextStream stream( &file );
while (!stream.atEnd())
{
curLine=stream.readLine();
if ((curLine!="EOV"))
{
if (curLine!="")
sumLine=sumLine+curLine+"\n";
}
else
{
lines[i]=sumLine;
sumLine="";
i++;
}
}//while
file.close();

//remove the last char which is always \n due to the way
//the file open parser (just above) works
lines[0].remove( (lines[0].length()-1) ,1);
lines[1].remove( (lines[1].length()-1) ,1);
lines[2].remove( (lines[2].length()-1) ,1);
lines[3].remove( (lines[3].length()-1) ,1);
lines[4].remove( (lines[4].length()-1) ,1);
lines[5].remove( (lines[5].length()-1) ,1);

stationNum=loc->toUnicode(lines[0]);
stationLoc=loc->toUnicode(lines[1]);
operatorName=loc->toUnicode(lines[2]);
operatorTel=loc->toUnicode(lines[3]);
installationDate=loc->toUnicode(lines[4]);
maintenanceDate=loc->toUnicode(lines[5]);

return (0);
}
else
return (1);

}

i have written it like this since the variable stationLoc might contain \n character since it stores an address. so i needed separator entries (EOV= end of variable).

so any ideas what might be wrong with saving?

jacek
15th November 2006, 20:49
am i using QTextCodec correctly?
Yes, but instead of "loc->setCodecForCStrings(loc);" write "QTextCodec::setCodecForCStrings(loc);" as this is a static method.

Maybe you should try this way:

void monitorForm::writeToGuiFile()
{
QString eov( "\nEOV\n" );
QFile file(guiFileName);
if ( file.open( IO_WriteOnly ) )
{
QTextStream stream( &file );
stream.setCodec( loc );
stream << stationLoc << eov
<< stationLoc << eov
<< operatorName << eov
<< operatorTel ...;
}
file.close();
}