|
class test
{
public:
CString str;
}
test oTest;
oTest.str = "abc";
When I try to write the above object 'oTest' to a simple file using fwrite and CFile and then try to read using fread, I am getting bad pointer in oTest.str
But when I replace the CString member with char array as "char str[50]" and try to write and read file, its working
So I want to know how to write and read a class object with CString member to and from a file
|
|
|
|
|
|
manoharbalu wrote:
When I try to write the above object 'oTest' to a simple file using fwrite and CFile and then try to read using fread, I am getting bad pointer in oTest.str Seeing the ACTUAL code would be helpful. Just sayin'...
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
fwrite only stores the memory contents of the class test - it doesn't care about the class definition except for it's size in memory.
CString stores the text in dynamic memory, only handle (pointer) to that memory is directly stored inside the CString object, therefore writing oTest just stores that pointer, but not the contents. If you use char[50] instead, that is a fixed-size array that is stored fully in your class test, and therewore fwrite will write it to file correctly.
The correct way to implement streaming for your test class in C++ is by overloading the streaming operators. See http://www.tutorialspoint.com/cplusplus/input_output_operators_overloading.htm[^]
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Windows 7 panning via a touchscreen used to scroll the client area for MFC Scrollview apps without any special code alterations but Windows 8.1 only scrolls the windows scrollbar while the view does not get scrolled until I invalidate the whole client area explicitly. I've tried various application types and am currently using the SysMets3 example from the Petzold Win32 "Programming Windows" book CD to study the behavior without MFC handling to see what is up.
Most Win32 scrolling examples are still basically the good old Petzold model when I follow the guidelines for "Legacy" apps in the Gestures and Touch MSDN technical articles so I'm assuming that approach is still supported. I've tried the disabling "Flicks" article and studied every line of code between current examples and the SysMets example and made sure there is case label for SB_THUMBTRACK and SB_THUMBPOSITION and all the other boilerplate case stuff.
In the end, debugging has revealed that the section that ultimately calls ScrollWindow() never runs because the scroll position and the scroll track position come into the WM_VSCROLL handler with the same value so the follow snippet no longer runs under Window 8.1 like it used to for touchscreens on previous OS versions...
...
si.fMask = SIF_POS;
SetScrollInfo (hWnd, SB_VERT, &si, TRUE) ;
GetScrollInfo (hWnd, SB_VERT, &si) ;
if (si.nPos != iVertPos)
{
ScrollWindow (hWnd, 0, cyChar * (iVertPos - si.nPos), NULL, NULL) ;
UpdateWindow (hWnd) ;
}
...
I'm testing this app on a Windows Surface (Gen 1) with Windows 8.1 with all the Windows Updates applied and Windows 8.1 Update installed back when the machine image was created. Visual C++ 2008 Professional with all known SP's installed.
I've even effectively removed all of the WM_VSCROLL code and tried returning from the WndProc with 0 and tried letting the default handler take it's course...either way the scrollbar moves anyway as if windows is setting the scroll position and scroll tracking position to the same value by some back door that breaks the ability to call ScrollWindow (or ScrollWindowEx) by traditional means (being able to determine how much to ScrollWindow().
Has anyone witnessed this and found a way around it without needing to add Touch/Gesture specific handlers in there?
Thanks in advance.
UPDATE: Just confirmed that this behavior continues to exist on Windows 10 Pro Technical Preview so it looks like Microsoft made some change at some point to make it almost impossible to benefit from the default legacy mapping of Touch Panning Gesture mappings to WM_VSCROLL messages without resorting to invalidating the entire client area.
Hopefully I'm missing some important detail and someone can set me straight.
modified 8-Jun-15 10:40am.
|
|
|
|
|
Just posting what I've resorted to for anyone else reading this in the future...
I decided to handle WM_GESTURE messages and provide the calls to ScrollWindow there since the scroll position and scroll handle position can be correctly processed. The caveat to all this is that it requires targeting Windows 7 and in my case, since I'm still using Visual Studio 2008 Professional, my IDE needed to bind to the Windows SDK v7.1 for the updated headers needed. Newer IDE's will already bind to a version of the Windows SDK that includes the necessary symbols for targeting Windows 7.
Another caveat is that newer versions of MFC supposedly provide support for gesture via virtual member functions, but no classwizard support (as of VS 2012 Pro), blah...blah...blah, so adapt your approach as your environment dictates.
|
|
|
|
|
well im trying to delete some nodes from my list and search for them using 2 different functions
problem is that deleteNode it just doesnt work (not even an error msgs)
when i activate the print list function it stays the same
could someoen help me with this?
deletes the node:
numStruct* deleteNode(numStruct** firstNode, numStruct* nodeToDelete)
{
numStruct* currNode = *firstNode;
numStruct* temp;
// if the list is not empty
if (*firstNode)
{
// the first node should be deleted
if (currNode == nodeToDelete)
{
*firstNode = (*firstNode)->Next;
free(currNode);
}
else
{
while (nodeToDelete != currNode->Next && currNode->Next)
{
currNode = currNode->Next;
}
if (nodeToDelete == currNode->Next && nodeToDelete)
{
temp = currNode->Next;
currNode->Next = temp->Next;
free(temp);
}
}
}
printf("\n\n");
}
this is my main:
case 2:
{
numStruct* t = anchorNode;
int arr[50] = { 261363 }, spot = 0;
printf("Please enter all the numbers you want to delete and end it with - 999 \n");
while ((arr[spot] != -999) && (spot != 50))
{
scanf("%d", &arr[spot]);
if (arr[spot] != -999)
{
spot++;
}
}
if (arr[spot] == -999)
{
arr[spot] = 261363;
spot--;
}
for (int i = 0; i < spot; i++)
{
int flag = 0;
while ((t->number != arr[i]) && (t->number != NULL))
{
t = t->Next;
if (t->number == arr[i])
{
flag = 1;
}
}
if (flag == 1)
{
deleteNode(anchorNode, t);
t = anchorNode;
}
}
break;
}
|
|
|
|
|
i'd start looking here:
numStruct* deleteNode(numStruct** firstNode, numStruct* nodeToDelete)
fn takes a pointer to a pointer to a numStruct.
but here:
numStruct* t = anchorNode;
...
deleteNode(anchorNode, t);
you're just passing a pointer to a numStruct.
|
|
|
|
|
i recently studied about this subject and i tried to code a program that does some stuff connected to it
but i ran into troubles while tryign to add more structs with numbers
i cutted mose of my program (to your convinient)
please help me to udnerstand my mistakes
http://pastebin.com/nyj3wtXT[^]
|
|
|
|
|
Please add some proper detail to your question and include your code here rather than offsite.
|
|
|
|
|
well i first created the code to work on the main but somehow somethign went wrong when i tried putting the codes in functions
<
every time i go to the function insertAtEnd but at the second loop of the while it just says error
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct numberNode
{
int number;
struct numberNode* Next;
};
typedef struct numberNode numStruct;
numStruct* createSong(int numbers)
{
numStruct* newSong = (numStruct*)malloc(sizeof(numStruct));
if (newSong)
{
newSong->number = numbers;
newSong->Next = NULL;
}
return newSong;
}
void insertAtEnd(numStruct** firstNode, numStruct* newNode)
{
numStruct* currNode = *firstNode;
// if the linked list is empty
// should put the new node as the first
if (!currNode)
{
*firstNode = newNode;
newNode->Next = NULL;
}
else
{
while (currNode->Next) // problem at the second loop
{
currNode = currNode->Next;
}
currNode->Next = newNode;
newNode->Next = NULL;
}
printf("\n\n");
}
numStruct* AddSongs(numStruct* anchorNode, numStruct* newNode)
{
int count = 0;
int numbers;
printf("\nPlease enter numbers and at the end enter -999\n");
do
{
count++;
_flushall();
numbers = 0;
printf("\n\n%d.\n\nnumber: ", count);
scanf("%d",&numbers);
if (numbers != -999)
{
newNode = createSong(numbers);
insertAtEnd(&anchorNode, newNode);
}
}
while (numbers != -999);
printf("\n\n");
}
int main(void)
{
numStruct* anchorNode = NULL;
numStruct* newNode;
AddSongs(&anchorNode, &newNode);
system("PAUSE");
}
|
|
|
|
|
Your code is almost correct, just a couple of errors in the main function.
The variable newNode have to be initialized to NULL to avoid errors and compiler complainings.
anchorNode and NewNode are already pointer to structures of type numStruct, so you have to pass them directly to AddSongs(), but you're passing their address (even if this sounds strange to me because you should have got a compiler error).
The correct code is this:
int main(void)
{
numStruct* anchorNode = NULL;
numStruct* newNode = NULL;
AddSongs(anchorNode, newNode);
system("PAUSE");
}
|
|
|
|
|
ok then if thats the case why
does this next function doesnt work after making the linked list
void printList(numStruct* firstNode)
{
numStruct* currSong = firstNode;
printf("\n\n-------------------------------------\n");
while (currSong)
{
printf("number= %d \n", currSong->number);
currSong = currSong->Next;
}
printf("-------------------------------------\n\n");
printf("\n\n");
}
which in main it will look like this
int main(void)
{
numStruct* anchorNode = NULL;
numStruct* newNode = NULL;
AddSongs(anchorNode, newNode);
printList(anchorNode);
system("PAUSE");
}
|
|
|
|
|
Because just the first variable anchorNode must be passed as pointer to struct pointer to have it assigned. Also AddSongs must pass it directly to insertAtEnd().
I report the whole program here:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct numberNode {
int number;
struct numberNode *Next;
};
typedef struct numberNode numStruct;
numStruct *createSong(int numbers)
{
numStruct *newSong = (numStruct *)malloc(sizeof(numStruct));
if (newSong)
{
newSong->number = numbers;
newSong->Next = NULL;
}
return newSong;
}
void insertAtEnd(numStruct **firstNode, numStruct *newNode)
{
numStruct *currNode = *firstNode;
if (!currNode)
{
*firstNode = newNode;
newNode->Next = NULL;
}
else
{
while (currNode->Next) {
currNode = currNode->Next;
}
currNode->Next = newNode;
newNode->Next = NULL;
}
printf("\n\n");
}
numStruct *AddSongs(numStruct **anchorNode, numStruct *newNode)
{
int count = 0;
int numbers;
printf("\nPlease enter numbers and at the end enter -999\n");
do
{
count++;
fflush(stdin);
numbers = 0;
printf("\n\n%d.\n\nnumber: ", count);
scanf("%d", &numbers);
if (numbers != -999)
{
newNode = createSong(numbers);
insertAtEnd(anchorNode, newNode);
}
}
while (numbers != -999);
printf("\n\n");
return NULL;
}
void printList(numStruct *firstNode)
{
numStruct *currSong = firstNode;
printf("\n\n-------------------------------------\n");
while (currSong)
{
printf("number= %d \n", currSong->number);
currSong = currSong->Next;
}
printf("-------------------------------------\n\n");
printf("\n\n");
}
int main(void)
{
numStruct *anchorNode = NULL;
numStruct *newNode = NULL;
AddSongs(&anchorNode, newNode);
printList(anchorNode);
system("PAUSE");
}
Check it against your code.
We pass a pointer to a variable when we want access the variable from the called function, in your case to assign to anchorNode the beginning of the memory list.
|
|
|
|
|
that worked perfectly thank you very much
|
|
|
|
|
there is one more thing that bothers me
1 more problem im having in my full program is to "delete" a part of the linked list
and reconnect it to the others by an input
i tried to do this next thing:
void deleteNode(numStruct** firstNode, numStruct* nodeToDelete)
{
numStruct* currNode = *firstNode;
numStruct* temp;
// if the list is not empty
if (*firstNode)
{
// the first node should be deleted
if (currNode == nodeToDelete)
{
*firstNode = (*firstNode)->Next;
free(currNode);
}
else
{
while (nodeToDelete != currNode->Next && currNode->Next)
{
currNode = currNode->Next;
}
if (nodeToDelete == currNode->Next && nodeToDelete)
{
temp = currNode->Next;
currNode->Next = temp->Next;
free(temp);
}
}
}
inside the main:
numStruct* t = anchorNode;
int arr[50] = { 261363 }, spot = 0;
printf("Please enter all the numbers you want to delete and end it with - 999 \n");
while ((arr[spot] != -999) && (spot != 50))
{
scanf("%d", &arr[spot]);
if (arr[spot] != -999)
{
spot++;
}
}
if (arr[spot] == -999)
{
arr[spot] = 261363;
spot--;
}
for (int i = 0; i < spot; i++)
{
int flag = 0;
while ((t->number != arr[i]) && (t->number != NULL))
{
t = t->Next;
if (t->number == arr[i])
{
flag = 1;
}
}
if (flag == 1)
{
deleteNode(anchorNode, t);
t = anchorNode;
}
}
the problem is that it doesnt really delete it
when i go beck to the menu and do the print function again it appears the same
|
|
|
|
|
There are a lot of errors and redundant code.
See the following solution, study it and go on on yourself now.
void deleteNode(numStruct **firstNode, numStruct *nodeToDelete)
{
numStruct *temp = NULL;
if (!*firstNode)
return;
if (*firstNode == nodeToDelete)
{
temp = *firstNode;
*firstNode = (*firstNode)->Next;
free(temp);
return;
}
for (numStruct * currNode = *firstNode; currNode->Next; currNode = currNode->Next)
{
if (nodeToDelete == currNode->Next)
{
temp = currNode->Next;
currNode->Next = (currNode->Next)->Next;
free(temp);
return;
}
}
}
void RemoveNodes(numStruct **anchorNode)
{
numStruct *t = *anchorNode;
int arr[50] = {0};
int spot = 0;
printf("Please enter all the numbers you want to delete and end it with - 999 \n");
for (spot=0; spot < 50; spot++)
{
fflush (stdin);
scanf("%d", &arr[spot]);
if (arr[spot] == -999)
break;
}
if (spot >= 50)
spot--;
for (int i = 0; i < spot; i++, t = *anchorNode)
{
while (t != NULL)
{
if (t->number == arr[i])
{
deleteNode(anchorNode, t);
break;
}
t = t->Next;
}
}
}
int main(void)
{
numStruct *anchorNode = NULL;
numStruct *newNode = NULL;
AddSongs(&anchorNode, newNode);
printList(anchorNode);
RemoveNodes(&anchorNode);
printList(anchorNode);
system("PAUSE");
}
|
|
|
|
|
thats brilliant thank you very very much!!
|
|
|
|
|
|
a random user wrote: the problem is that it doesnt really delete it So have you stepped through each line of code using the debugger? Short of doing that, asking questions and pasting others' code snippets into your project are just a waste of time.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
esecuse me kind sir but thats not really true after each and every answer i get i dont just copy it, i also try to udnerstand where i went wrong and try to udnerstand it , I am learning from my mistakes after all i only started with the programming in this year
|
|
|
|
|
Hi
I have a CAsyncSocket Class as a member of a CWinThread Class
To start the TCP/IP conversation I do a PostThreadMessage to a Worker UI thread
which has CAsyncSocket as a member and do a CAsyncSocket::connect
However When the FrameWork calls both the CasyncSocket::OnConnect and CAsynScoket::OnSend
I can tell Visual Studio Debugger -> Thread display that the thread id is that of the main thread
I am having problems doing the subsequest CAsyncSocket::Send and am guessing this more than likely the issue
Thanks
|
|
|
|
|
The
CASyncSOcket notifications OnConnect OnSend owned by main thread
|
|
|
|
|
Your question is not clear...
|
|
|
|
|
I contacted Joseph Flounder and He explained That CAsyncSocket notifications
OnSend, OnConnect are intitally received in the context of the main thread
I detach the socket do a PostThreadMessage to the Thread with the CAsynsocket derived
member do a Attach
and from that point on all notification are received in The UI CWinThread
|
|
|
|