|
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
|
|
|
|
|
Hello.
This is one (AFAIK the only) way to do it:
<code>
FILE *fd = fopen("blabla", "w");
if(_chsize(_fileno(fd), (long) 12345L) == 0) {
//OK
else {
// not OK
}
fclose(fd);
</code>
Before I get flamed for my response:
1.
No, I do not check fd for errors. This is just a code snippet.
2.
Yes, it uses old C functions.
Kakan
|
|
|
|
|
I could do it with _chsize function, as Kakan said.
But the file should be opened in "r+" mode, instead of "w".
I have not known the _fileno function to get the handle from FILE object.
Thanks for the valuable reply.
Sarvan AL
-- modified at 3:44 Thursday 12th January, 2006
|
|
|
|
|
Hello.
You can change the file size as long as you have the file opened for writing, so "r+" (append) is OK.
The _fileno() function is in the runtime library, so you do have it, for sure. It's declared in stdio.h.
If it's missing, the try using fileno() instead.
Kakan.
|
|
|
|
|
How to refresh the intellisense information about functions that are on a file OUTSIDE the project, that is linked using #include?
(delete the file xxx.ncb don't work)
Have a nice code day
|
|
|
|
|
_Russell_ wrote: OUTSIDE
put it INSIDE
Jesus Loves <marquee direction="up" height="50" scrolldelay="1" step="1" scrollamount="1" style="background:#ffaaaa">
--Owner Drawn
--Nothing special
--Defeat is temporary but surrender is permanent
--Never say quits
--Jesus is Lord
|
|
|
|
|
Hi.
I'm building an MFC app, and at this time trying to add printing facilities.
What i need to do, is to print the image (all the windows of my app show different images) so that it fits the printing page...i've been trying to find the correct calculation (working on OnPrepareDC) with different page/window/screen measures (GetDeviceCaps(...)), but i don't seem to get it.
The pseudo code of OnPrepareDC:
<br />
SuperView::OnPrepareDC(pDC, pInfo);<br />
if (IsPrinting)<br />
{<br />
pDC->SetMapMode(MM_ANISOTROPIC);<br />
CClientDC dc( this );<br />
CSize szScreen = dc.GetDeviceCaps(...);<br />
pDC->SetWindowExt( szScreen );<br />
<br />
CSize szPage = pDC->GetDeviceCaps(...);<br />
CSize szfinal = SOME_CALCULATIONS(szScreen, szPage);<br />
pDC->SetViewportExt( szFinal );<br />
}<br />
...<br />
If someone understood the message, and knows something about it...will save my life.
Thanks,
Alberto
|
|
|
|
|
What's the siplest way to read from and write to the registry?
Thanks
KevinSheedy
|
|
|
|