Results 1 to 14 of 14

Thread: unable to save QCStrings properly in a buffer

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: unable to save QCStrings properly in a buffer

    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:

    Qt Code:
    1. void readingTheShMem()
    2. {
    3. int tOTL=0;
    4. int tONL=0;
    5. int tSNL=0;
    6. int tSLL=0;
    7. int tIDL=0;
    8. int tMDL=0;
    9. int readingLength;
    10.  
    11. //lock the file for writing
    12. memset (&lock, 0, sizeof(lock));
    13. lock.l_type = F_WRLCK;
    14. fcntl (fd, F_SETLKW, &lock);
    15.  
    16. struct ShMem *infoVar=(ShMem*)((char*)file_memory);
    17.  
    18. tOTL = infoVar->oTL;
    19. tONL = infoVar->oNL;
    20. tSNL = infoVar->sNL;
    21. tSLL = infoVar->sLL;
    22. tIDL = infoVar->iDL;
    23. tMDL = infoVar->mDL;
    24. readingLength = tOTL + tONL + tSNL + tSLL + tIDL + tMDL ;
    25. char *readingStrs[readingLength];
    26. printf("reading length = %d\n", readingLength);
    27. printf("the infoVar->oAndS_str reading points to=%p\n",&(infoVar->oAndS_str));
    28. memcpy(readingStrs, &(infoVar->oAndS_str), readingLength);
    29.  
    30. //unlock the file to allow access
    31. lock.l_type = F_UNLCK;
    32. fcntl (fd, F_SETLKW, &lock);
    33.  
    34. char *tOT[tOTL]; memcpy(tOT,readingStrs,tOTL);
    35. printf("tOTL = %d and oT ptr = %p\n", tOTL, readingStrs);
    36. char *tON[tONL]; memcpy(tON,(readingStrs+tOTL),tONL);
    37. printf("tONL = %d and oN ptr = %p\n", tONL, (readingStrs+tOTL));
    38. char *tSN[tSNL]; memcpy(tSN,(readingStrs+tOTL+tONL),tSNL);
    39. printf("tSNL = %d and sN ptr = %p\n", tSNL, (readingStrs+tOTL+tONL));
    40. char *tSL[tSLL]; memcpy(tSL,(readingStrs+tOTL+tONL+tSNL),tSLL);
    41. printf("tSLL = %d and sL ptr = %p\n", tSLL, (readingStrs+tOTL+tONL+tSNL));
    42. char *tID[tIDL]; memcpy(tID,(readingStrs+tOTL+tONL+tSNL+tSLL),tIDL);
    43. printf("tIDL = %d and iD ptr = %p\n", tIDL, (readingStrs+tOTL+tONL+tSNL+tSLL));
    44. char tMD[tMDL]; memcpy(tMD,(readingStrs+tOTL+tONL+tSNL+tSLL+tIDL),tMDL);
    45. printf("tMDL = %d and mD ptr = %p\n", tMDL, (readingStrs+tOTL+tONL+tSNL+tSLL+tIDL));
    46.  
    47. printf("oT = %s\n",tOT);
    48. printf("oN = %s\n",tON);
    49. printf("sN = %s\n",tSN);
    50. printf("sL = %s\n",tSL);
    51. printf("iD = %s\n",tID);
    52. printf("mD = %s\n",tMD);
    53.  
    54. operatorTel = QString::fromLocal8Bit((const char*)tOT);
    55. operatorName = QString::fromLocal8Bit((const char*)tON);
    56. stationNum = QString::fromLocal8Bit((const char*)tSN);
    57. stationLoc = QString::fromLocal8Bit((const char*)tSL);
    58. installationDate = QString::fromLocal8Bit((const char*)tID);
    59. maintenanceDate = QString::fromLocal8Bit((const char*)tMD);
    60.  
    61. //do smth with these variables now
    62.  
    63. }
    To copy to clipboard, switch view to plain text mode 

    and the output i get

    Qt Code:
    1. reading length = 41
    2. the infoVar->oAndS_str reading points to = 0xb7736168
    3. tOTL = 8 and oT ptr = 0xbfcfa020
    4. tONL = 4 and oN ptr = 0xbfcfa168
    5. tSNL = 4 and sN ptr = 0xbfcfa20c
    6. tSLL = 5 and sL ptr = 0xbfcfa2b0
    7. tIDL = 10 and iD ptr = 0xbfcfa37d
    8. tMDL = 10 and mD ptr = 0xbfcfa517
    9. ϿПϿ�Ͽ ϿPϿ8hihi1234hello10/10/200022/22/2000;
    10. oN = |
    11. sN =
    12. sL = �h
    13. iD = Ͽ
    14. mD =
    To copy to clipboard, switch view to plain text mode 
    do you have any idea where my train of thinking is faulting?
    nass
    Last edited by nass; 14th November 2006 at 13:32.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.