|
Nice, but i want the size to be based on the number of characters the user enters.
how to get it?
myChars.resize( anysizeyoulike );// anysizeyoulike should be differing based on the No. of char. the user is entering. getchar() will be of any use?
regards,
Rookie
Installing MFC...2% complete
|
|
|
|
|
If you're getting characters from the keyboard then try something like:
#include <vector>
typedef std::vector<char> TCharArray;
TCharArray myChars;
while( char ch=getchar() )
{
myChars.push_bach(ch);
}
myChars.size() will tell you how many chars you've got
--
Help me! I'm turning into a grapefruit!
Buzzwords!
|
|
|
|
|
if you need to tell the "anysize", why not this work?
do
{
//enter anysize;
anysize =4; //user entered
char *str= new char(anysize);
}while
0x0400:
"But your mind is very complex, very tricky. It makes simple things complicated. -- that's its work. And for centuries it has been trained for only one thing: to make things so complicated that your life becomes impossible."- Osho
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[V]--
|
|
|
|
|
That's where you should use a string wrapper like the MFC/ATL CString or C++ std::string.
Regards,
Nish
|
|
|
|
|
Use string or MFC CString.
Using them would help you in all of this:-
1) Don't have to bother about freeing memory after usage.
2) Potential crashes by working directly with pointers and possible
buffer overflows.
Both of them become issues when you write a program which would grow into several 1000 lines of code and ultimately end up in ugly event of crashes
on customer machines.
As part of coding style, make them part of your company or personal coding
practice and follow them from the day one you start working on a project.
If not followed you would endup in unmaintainable code, with memory
allocations at one place and deallocation a mile away.memory leaks and troubles which could be avoided at first place. That's were c++ comes to
your rescue, use them to the fullest wherever you can.
Vipin - MVP
|
|
|
|
|
You're asking one of two things:
Q1) how do I create an adjustable-length string to read data into?
A - use something like std::vector instead
Q2) how do I read in an arbitrary length of user data and minimise the amount of temporary storage required?
A - basically, if you don't know in advance how much data there will be, you can only make an educated guess, then shorten the string to the correct size at the end.
To keep things to an *absolute minimum*, you read a character at a time and resize and copy the string each new character. However, this is theoretical - it would be dog slow and would probably fragment memory hideously for long message.
A common solution is to start off with a buffer (use a std::vector again) and read as many bytes in a chunk from your socket as it would take to fill this. If you've finished reading, stop and shorten your vector.
If there's more text, increase the length of the string and read more. You could increase by the same amount (so your string lengths are N, 2N, 3N etc) or double it each time (giving lengths of N, 2N, 4N etc), or any other method.
What you want to do is minimise the amount of memory copying and number of socket reads.
Hope that helps (homework? )
Kev
|
|
|
|
|
I totally agree. Nevertheless, I wrote EXACTLY what he wanted (and I hope it works, I didnt test it)
I love buffered streams...
Cheers,
Sebastian
--
Contra vim mortem non est medicamen in hortem.
|
|
|
|
|
hmm?
<br />
char inChar[2] = {0,0};<br />
char* pTemp = 0;<br />
char* pText = 0;<br />
int length = 2;<br />
<br />
inChar[0] = fgetc(stdin);<br />
<br />
pText = (char*)malloc(sizeof(char)*length);<br />
strcpy(pText,&inChar[0]);<br />
inChar[0] = fgetc(stdin);<br />
<br />
while((int)inChar[0] != 10)<br />
{<br />
length++;<br />
pTemp = pText;<br />
pText = (char*)malloc(sizeof(char)*length);<br />
strcpy(pText,pTemp);<br />
pText[length-2] = inChar[0];<br />
pText[length-1] = 0;<br />
free(pTemp); <br />
inChar[0] = fgetc(stdin);<br />
}<br />
free(pText);<br />
Its optimizable. But I didnt have time to optimize the first step out
Cheers,
Sebastian
--
Contra vim mortem non est medicamen in hortem.
|
|
|
|
|
not fully structured.. i typed just like that...
typedef struct node
{
char chr;
int index;
struct node* nChr;
}chatSet_t;
char *makeString(struct node* Start,int size)
{
int j=0;
struct node* newStr =Start;
char *myString =new char(size);
while(j<size)
{
myString[j]=newStr->chr;
newStr=newStr->nChr;
j++;
}
myString[size]='\0';
return(&myString[0]);
}
int main(int argc, char* argv[])
{
char *actualString;
struct node* head = new (node);
struct node* newString = new (node);
int i=0;
head=newString;
char x;
do
{
x = getchar();
newString->chr =x;
newString->index =i;
newString->nChr =new(node);
newString=newString->nChr;
i++;
}while(x!=10);
actualString = makeString(head,i);
printf("\n%s",actualString);
getch();
return 0;
}
i did it.. but just not error proof
0x0400:
"But your mind is very complex, very tricky. It makes simple things complicated. -- that's its work. And for centuries it has been trained for only one thing: to make things so complicated that your life becomes impossible."- Osho
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[V]--
|
|
|
|
|
Hello,
I don't know why nobody give the answer: std::istringstream . This class is designed to build strings of unknown length by appending segments to the buffer (stream) just like you would use std::cout . The code would look like:
std::istringstream istrInputString;
char cBuf[100] = {0};
while(
{
istrInputString << cBuf;
}
See MSDN[^] for more details.
Hope this helps.
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
But you will need to make sure cBuf is null terminated. Using std::vector with push_back or append will work better.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Hello,
Since he indicated that he was using normal characters, there is no need to use vectors as this complicates the matter of displaying strings.
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
I have to agree with vipinasda here. Use CString (MFC) or std::string (STL) and save yourself a lot of hastles. My preference is to use STL where possible.
Steve
|
|
|
|
|
I'm using a call to fopen with the permissions of "a+" and writing logging information to the opened file. Unfortunately I'm now having to scale this to multiple programs and users. Is there some other permissions or even a different open file call that I can use that will ensure only one writer to the file and block any other opens? It seems that my current method allows multiple writers at the same time with the result that logging information is lost. Thanks.
Chris Meech
I am Canadian. [heard in a local bar]
Remember that in Texas, Gun Control is hitting what you aim at. [Richard Stringer]
Nice sig! [Tim Deveaux on Matt Newman's sig with a quote from me]
|
|
|
|
|
Firstly, if you are programming in MFC,
please use CFile, instead of low level calls. You had like to be free of
having to track resource handles and explicitly calling fclose(...) for
every fopen.
With CFile you can use the CFile::shareExclusive to accomplish what you
would want.
At API level you could pass the 3rd parameter to CreateFile(...) as 0, it is
actually the dwShareMode.
If you plan to use the CRT routines, still , fo ahead and have a look at
_locking(...)
|
|
|
|
|
Thanks for the tips. Converting to a CFile looks to be the way to go. The fopen calls are from 10 year old library code that's never been touched.
Chris Meech
I am Canadian. [heard in a local bar]
Remember that in Texas, Gun Control is hitting what you aim at. [Richard Stringer]
Nice sig! [Tim Deveaux on Matt Newman's sig with a quote from me]
|
|
|
|
|
it wont help
the problem is not your fopen but the multiple process access.
u can several things. here are 2:
1. lock on a mutex with a name derived from the file's name.
2. if u can't open, Sleep for 10ms and retry repeatedly
if u just enable other processes to touch the file while u write it, u will very quickly garble the file
btw - in my openion fopen is much better than CFile
|
|
|
|
|
hmm , then what are those flags built in the operating system for? They are just for that. Don't reinvent the wheel with mutexes and make the code a mess.
Vipin - MVP
|
|
|
|
|
You are wrong here.
Sharing flags are supplied to answer any possible user's need in creating/opening any kind of stream(file/pipe/sock...) but nobody said you could trust the sharing flags to automatically synchronize the read/write operations.
Thread synchronization is the user's responsibility and using a named mutex is very simple and generic solution.
In cross-process scenario the solution must include a lock/spin on a global object so these are the options:
1. The file itself, that can be locked on opening, so the other task should spin on CreateFile till success.
2. Lock on a named mutex
3. Use some other shared info like the registry
|
|
|
|
|
I need to write message handler to handle the KillFocus event of 10 text boxes.
How to do using the ON_NOTIFY_RANGE macro? Is there any other way to do?
|
|
|
|
|
From the MSDN website:
ON_NOTIFY_RANGE
If you need to process the same WM_NOTIFY message for a set of controls, you can use ON_NOTIFY_RANGE rather than ON_NOTIFY. For instance, you may have a set of buttons for which you want to perform the same action for a certain notification message.
When you use ON_NOTIFY_RANGE, you specify a contiguous range of child identifiers for which to handle the notification message by specifying the beginning and ending child identifiers of the range.
ClassWizard does not handle ON_NOTIFY_RANGE; to use it, you need to edit your message map yourself.
The message-map entry and function prototype for ON_NOTIFY_RANGE are as follows:
ON_NOTIFY_RANGE( wNotifyCode, id, idLast, memberFxn )
where the italicized parameters are replaced with:
wNotifyCode
The code for the notification message to be handled, such as LVN_KEYDOWN.
id
The first identifier in the contiguous range of identifiers.
idLast
The last identifier in the contiguous range of identifiers.
memberFxn
The member function to be called when this notification is sent.
Your member function must be declared with the following prototype:
afx_msg void memberFxn( UINT id, NMHDR * pNotifyStruct, LRESULT * result );
where the italicized parameters are:
id
The child identifier of the control that sent the notification.
pNotifyStruct
A pointer to the notification structure, as described above.
result
A pointer to the result code you'll set before you return.
|
|
|
|
|
Hi,
Will this Suffice
ON_CONTROL_RANGE(EN_KILLFOCUS,IDC_EDIT1, IDC_EDIT10, OnKillFocus )<br />
virtual void OnKillFocus (CWnd *pWwd);
IDC_EDIT1 and IDC_EDIT10 they should have continuous values in the resource.h.
Differnt Ways
You can also try this map the WM_COMMAND and trap the EN_KILLFOCUS or if the edit controls are derived from a common class then trap its WM_CHILDNOTIFY and in that the EN_KILLFOCUS , these are the differnet ways in which you could centralize you code.
Regards
FarPointer.
-- modified at 9:21 Wednesday 11th January, 2006
|
|
|
|
|
Using ON_CONTROL_RANGE, I could get the things to happen, as per FarPointer's idea.
But the handler should be defined as
" virtual void OnKillFocus (UINT nID) //in the header".
Thanks for the valuable response.
Sarvan AL
|
|
|
|
|
Hi,
How can I set the End Of File of a file which is opened using the fopen in "r+" mode? Is there an equivalent function for "SetEndofFile" to use with fopen and FILE?
If I try to use CFile and its SetFileLength, I am getting the "sharing access violation" error. But no other function is using the file.
Thanks in advance.
sarvan al
sarvan_tech@rediffmail.com
|
|
|
|
|
I dont think there is any thing called end of file "marker". End of file means that there is no more data in that file.
Sarvan AL wrote: I am getting the "sharing access violation" error
Were you able to open the file?
-Prakash
|
|
|
|