eranga262154 22 Junior Poster

That is really helpful to me. Thanks.

As there example explain, I try to send a message and depend on it destroy the window. But in my case the window is the console. If I get correct in my application I can't destroy the window. Right? So that mean the window destroyed automatically....

eranga262154 22 Junior Poster

I'm not clear what Ancient Dragon says. You says that, according to my code I can't send message to the window. I've try it with PostMessage() with the window handler. So....

eranga262154 22 Junior Poster

And he doesn't have a message pump so the window will never get any messages even if it were created.

I can send messages to the window. Send a message to close the window, (WM_CLOSE) and try to destroy the window. But not work. I can send messages to it.

eranga262154 22 Junior Poster

He use API i tought that is for windows application.

Yes I use windows API

And must have WinProc.

In API I can't use it, isn't it?

eranga262154 22 Junior Poster

CreateWindowEx does not throw an exception on error so there is no point to the try/catch block. If the function fails then you have to call GetLastError() to find out the error number, and you can pass that to FormatMessage() to get a human-readable error message.

Yes there is no exception thrown. I've just use it for my clear work out, and also I've never use the GetLastError() because few time I'm confused that at which place should I use it.

I don't think you want to use the console's window handle as the parent of a Windows GUI program. Instead, pass NULL as that parameter.

Used the console window as the main window because my application also console based application.

eranga262154 22 Junior Poster

But I can't use WindowProc, because I use windows API here in my code.

eranga262154 22 Junior Poster

well both the methods are good....first with the integer array suggested by gabs and second with structure of two integers suggested by Slaem...

Seems to me struct is good.

but i think it is better to use the method suggested by your friend...sending two integers by reference... i guess u dont know about the concept of sending the variables by reference...do you???

Handling variables either by reference or by value is not a question to me, may be I don't have better idea about them like people here.


user doesn't
call any function...it is the programmer who decides how to call the function....if you want to call the function by the string entered by the user you can still do that by sending it to the function with the two integers (pass by reference)...user will have no information about them....and how the function was called...

Ok, I got it. Actually user mean anyone who read my code. I want to hide information as much as possible.

eranga262154 22 Junior Poster

> That mean I have to return two int values from the function. How can I do that?
So how does your first post not achieve that?

Or you could contrive an answer with

struct twoint { int a, int b };
twoint func ( ) {
  twoint ans = { 1, 2 };
  return ans;
}

Yes, I've try the struct. Seems that is the best option to me. Use of bool return value seems useless.

eranga262154 22 Junior Poster
// Create window
	try
	{
		m_hwnd_RTFBox = CreateWindowEx(
							WS_EX_APPWINDOW,
							RICHEDIT_CLASS,
							"RichTextWindow",
							WS_BORDER|ES_MULTILINE,
							0,
							0,
							100,
							100,
							::GetConsoleWindow(),
							NULL,
							0,
							NULL);
	}
	catch(...)
	{
		std::cerr << "Can't create the window handler." << std::endl;
	}
// Processing done here
	::SetWindowText(m_hwnd_RTFBox, rtf.c_str());
	int recCount = GetWindowText(m_hwnd_RTFBox, pBuffer, 1024);
	
	if(recCount > 0)
	{
		string strPlr(pBuffer, recCount);
		cout << strPlr;
	}

// Want to destroy the window here
eranga262154 22 Junior Poster

Hi all,

I've create a window using CreateWindowEx windows API and do some work on it. Thats fine. But for the safer operation at the end of all the processing I want to close/destroy the window.

I've try this,

BOOL clsWin = DestroyWindow(m_hwnd_RTFBox);
	if(clsWin != 0)
	{
		printf("SS");
	}

Here m_hwnd_RTFBox is the handle to the window. On the above code there is no compile error, but gives a runtime error and saying there is a unhandled exception on the first line. Where I'm going wrong. I found this from the MSDN, but it wont work.

eranga262154 22 Junior Poster

what do you mean?

This is what I want.

I have a function that calculate two int values. User call this function with a string, and he gets two int values after doing all the processing inside the function. That mean I have to return two int values from the function. How can I do that?

eranga262154 22 Junior Poster

Actually I want to call the function only using a string.

eranga262154 22 Junior Poster

Hi all,

I have a function that calculated two int values. I call the function only by using a string and calculated those two int values. I want to return that two int values from the function when I call it with a string.

Look at the following function,

bool CharWordCount(string rtf, int& word_count, int& char_count)
{
// Body of the function
// do the processing and find the number of chars and
// number of words of the string
}

One of my friend say i can do it as above. using the bool type return value. Can you guys give me a help how to do that.

eranga262154 22 Junior Poster

The isspace(int ch) function returns nonzero if ch is a whitespace character, including space, horizontal tab, vertical tab, formfeed, carriage return, or newline character; otherwise, zero is returned.

Yep, I go through the MSDN and found the way it works. Thanks.

eranga262154 22 Junior Poster

No sweat, dude. Glad to help

Yep, I learn a lot from those replays. Thanks again

eranga262154 22 Junior Poster

Yep, seems it is more reliable than use of few number of if() conditions. Output also seems much better there. Thanks pal.

eranga262154 22 Junior Poster

Yep, I got it. Seems it is much better that use of (... == ...) style. I'll try it now.

eranga262154 22 Junior Poster

But seems isspace() ignored both white spaces and tab spaces. Right?

eranga262154 22 Junior Poster

Seems both codes are same. And how to handle tabs there.

eranga262154 22 Junior Poster

Use of an array can be more works. What it the my code odd you can see. Please let me know.

eranga262154 22 Junior Poster

Dealing with the user inputs wont difficult. Anyway I'll check it. What I'm really worried about is the way I find the characters of the string.

eranga262154 22 Junior Poster

Hi,

I want to count the number of characters of a string. Try to do it as follows.

int iSpace = 0;
	int iLength;

	string strTemp("This is a line                      of text");
	iLength = strTemp.length();

	cout << iLength << endl;

	for(int i = 0; i < iLength; i++)
	{
		if((strTemp.at(i) == ' ') || (strTemp.at(i) == '\t') || (strTemp.at(i) == '\0'))
		{
			iSpace ++;
		}
	}
	printf("\nNumber of characters %d", (iLength - iSpace));

Can you guys comments on my code, either this way is ok or bad attempt

eranga262154 22 Junior Poster

Yes, I use it but not works.

eranga262154 22 Junior Poster
std::stringstream ss(line);

Yes, I check your code as well. But is this valid. Got an compile error.

eranga262154 22 Junior Poster
In    that         case   this   sentence    has 27 words.

That is one of the big problem I've got. Multiple such instances(like spaces and tab, two spaces, and such..). So please look at my code, how I try to avoid it.

eranga262154 22 Junior Poster

Number of Words = Number of space + 1

Yes that's what I'm going to do. Find the number of spaces, tabs, carriage returns of the string. I can get the string from the rich edit control. How about the following attempt.

bool isLastCharBlank = true;
	int iWordCount = 0;
 
	char * szTemp = szInputString; 
 
	while(*szTemp) 
	{
		// Whitespase, tab, newline and carriage return
 		if ((*szTemp == ' ') || (*szTemp == '\n') || (*szTemp == '\r')) 
		{
			isLastCharBlank = true;
		}
 
		else if (isLastCharBlank) 
		{
 			iWordCount++;
			isLastCharBlank = false;
		}
 		szTemp++;
	}
	printf("Number of words %d",iWordCount);
eranga262154 22 Junior Poster

Hi all,

I want to find the number of words in a rich edit control. There is no direct class member to do this. So I think if I can count the number of word break and line brake then it is easy. But it is also not easy. On the MSDN also it is not well documented. Can you guys give me a clue.

Thanks.

eranga262154 22 Junior Poster

DLLs are much like normal static libraries. Create a header file with the dll functions you have exported and include it in your console project, then add the .lib file that the compiler created for the dll in the console project just like you would any other project.

I use a conditional compile preprocessor directives in the header file so that it can be included in both the dll build and the console application build

// header file
#if defined(DLL_BUILD)
#define MYAPI __dllexport
#else
#define MYAPI __dllimport
#endif

void MYAPI foo(std::string& datastr);

Thanks,

I've read few articles and get really mess with this header file and stuff. I think it is better to find a complete document on dll and read it first. I collect a document and going to read it tonight. :)

eranga262154 22 Junior Poster

Hi all,

For later use I want to create dll file, internally using MFC. I used MFC because it is so easy to do my job. So used the MFC-DLL Visual C++ project and code my project. Its ok.

Later add another Console Application which support MFC as well. I want to use it to send data to the dll externally. Simply pass a std::string to dll, and there use that string for processing.

My question is how can I connect those two projects together. How to send data to dll application from the console application.

eranga262154 22 Junior Poster

Yes that is true. First parameter points to either CString object or null-terminated string. So here I have to use a null terminated string. Now its clear to me.

eranga262154 22 Junior Poster

Ya, that is true. But can't do this in my application with strRTF string without null-terminating.

AfxMessageBox(strRTF,MB_OK);

Not work,

AfxMessageBox(strRTF.c_str(),MB_OK);

Works.

eranga262154 22 Junior Poster

Actually it doesn't really matter about that error because cout can not print to any MFC (or non-MFC) window. The output is just tossed into the bit bucket by the os. If you want to see debug messages then use MessageBox().

Even I use a MessageBox() I have to use a null-terminated string there. :(

eranga262154 22 Junior Poster

Hey, I found it. That strRTF should be a null-terminated string, so I change the line 21 as follows.

cout << strRTF.c_str();

But why it is necessary. Work on console application and cout is a standard iostream object.

eranga262154 22 Junior Poster

I'm not clear. I got it in this way, line 16 can be effect on line 21? I've comments the line 16 and still the issue is there.

eranga262154 22 Junior Poster

Of course. And also preceding everything with std:: but still it not work. Here is my code, why I'm confusing is the first cout works, but not the second one.

if(m_rtfCtrl.CreateEx(WS_EX_APPWINDOW, WS_BORDER|ES_MULTILINE, CRect(10,10,200,200), &x , 1))
		{
			CString ss;
			CFile rtfFile;
			BOOL err = rtfFile.Open("G:\\Work On\\CPP\\RTFControl\\TestFile.rtf", CFile::modeReadWrite, NULL);

			int iLength = rtfFile.GetLength();// Data length
			char *pBuffer = new char[iLength];// Data buffer

			rtfFile.Read(pBuffer, iLength);
			CString rtf(pBuffer);

			m_rtfCtrl.SetWindowText(rtf);
			m_rtfCtrl.GetWindowText(ss);

			std::cout << ss << std::endl;

			// CString into string
			std::string strRTF(ss.GetString());
			
			std::cout << strRTF << std::endl;

		}
eranga262154 22 Junior Poster

Ok, I've done it in this way.

std::string strRTF(ss.GetString());

and no compiled error at all.

To check it simply I used

cout << strRTF;

, here I got a compile error.

binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

I can't find why is that. Everything is done on console application with MFC support. So this should work.

eranga262154 22 Junior Poster

Thanks,

How about this way...

string MyStr( (const char*)MyCStringObject );

eranga262154 22 Junior Poster

Hi all,

What is the easiest way to convert a CString into a standard C++ sting.

Thanks

eranga262154 22 Junior Poster

Ok, here what I've done up to now.

First define CWnd object and CRichEditCrtl objects as follows.

CWnd * m_rtfWnd;
	CRichEditCtrl * m_rtfCtrl;
CFile rtfFile;
	BOOL err = rtfFile.Open("G:\\Work On\\CPP\\RTFControl\\TestFile.rtf", CFile::modeReadWrite, NULL);

	if(err !=0)
	{
		int iLength = rtfFile.GetLength();// Data length
		char *pBuffer = new char[iLength];// Data buffer

		rtfFile.Read(pBuffer, iLength);
		CString rtf(pBuffer);

		m_rtfCtrl->Create(WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE, CRect(100,100,100,100), m_rtfWnd, 1);// Associate with CRichEditCtrl
		m_rtfCtrl->SetWindowText(rtf);// Set the window text

		int i, nLength, nCount = m_rtfCtrl->GetLineCount();
		CString strText, strLine;

		for(i = 0;i < nCount;i++)
		{
			nLength = m_rtfCtrl->LineLength(i);
			m_rtfCtrl->GetLine(i, strText.GetBuffer(nLength));

			strLine.Format(("i %d '%s' \n", i, strText.GetBuffer(0)));
			AfxMessageBox(strText, MB_OK);
		}
	}

I got an error when I use 'create' to construct rich edit control. I can't find any error there. Do you guys have any idea about it.

eranga262154 22 Junior Poster

Yes, I'm going to work it on with MFC. What you think about it? Is it difficult, first time I'm going to do this.

eranga262154 22 Junior Poster

Hi all,

I want to read a Rich Text Format file and get texts form it only. So, what I have done is according to RTF specification start to code detecting tags. Actually it is too hard, because RTF format has more that 1000 tags. If I going to find each of them it will take a long time.

So I want to do it in shorter time. Can I do it with CRichEditCtrl?

eranga262154 22 Junior Poster

Hi all,

I want to store some data pairs. A variable name and the value. I've try it in this way.

//data map testing
map<string, int> my_map;//map
map<string, int>::iterator my_it;//map iterator

my_map["first"]=10;
my_map["second"]=12;

string ss = "third";

my_it = my_map.find(ss);
if(my_it == my_map.end())
{
	//insert new value
	my_map["third"] = 15;
}
else
{
	//update the exsiting value
	my_map.erase(my_it);
}

I think my code is clear to you. Insert string,int pair. Before that I want to check that same string are in the map. If so I want to change the value only, with the existing one, not the variable name. Thats the point I've stuck. Can you guys give me a help on this.

eranga262154 22 Junior Poster

Yep, you are right. I agreed with you that I can use strings in MFC, no question about that. And also I feel that learning pure/standard C++ is more better.

What I'm confusing is that, as far as I found from several articles, as a simple standard CStrings use in MFC projects, not strings. I don't know how far I'm clever on this sentences actually.

eranga262154 22 Junior Poster

Thanks, your way is working.

One thing I'm confusing. I'm using a string type variable, not a CString. Actually in an MFC application.

eranga262154 22 Junior Poster

Hi all,

I have a string(not a CString), actually a file path. As an example,

c:\Test\MyProject\G00062_002_01.srf

Then in following way get some data.

string FullPath = c:\Test\MyProject\G00062_002_01.srf
string filePath = FullPath.GetFileTitle();

		string groupID;		
		string sessionId;	
		string partID;		

		groupID = filePath.substr(1, 5);
		sessionId = filePath.substr(7, 3);
		partID = filePath.substr(11, 2);

So my outputs are 00062, 002, 01 respectively as string type. I want to add those values in a database as int type. How should I do this conversion.

Where I'm confusing is that, outputs are in string format. But actually there are some integers.

eranga262154 22 Junior Poster

how to get the program to run

If you don't know how to run the program it is better to do some tutorials before doing this, I think. Isn't it?

eranga262154 22 Junior Poster

> if(ss.ReverseFind('\\')== 8) What does this return if there is no \\ ?

That what I'm really worried. That number is not fix always. It means, \\ can be missing in such instances.

I've try something like this also,

CString aa = fileName.Right(fileName.GetLength() - (fileName.ReverseFind('\\') + 1));

Actually it gives the file path. Then use ReverseFind() again and find the '.', use a Left() to get the name only.

As you say, if the extension is missing this wont work.

eranga262154 22 Junior Poster

I have tried one thing. Use ReverseFind() method of CString class to find the last '\' sign. Then I used the Right() to get the required file name. I can use Right() easily because file name length is always fixed.

CString ss = "F:\\Files\\ReadFile.txt";
	CString aa;

	if(ss.ReverseFind('\\')== 8)
	{
	AfxMessageBox(ss.Right(12), MB_OK);
	aa = ss.Right(12);
	}

	if(aa.ReverseFind('.')==8)
	{
		AfxMessageBox(aa.Left(8), MB_OK);
	}

Actually this code gives the file name, ReadFile.

But there is a real issue, on the first 'if' condition. First part of the path, that is "F:\\Files" is not fixed. So in the first 'if' condition number 8 can be change depend on the file selection on my PC. How can avoid that.

eranga262154 22 Junior Poster

Ok, at the end I have done it myself using MSDN. Used CFindFile class and FindNextFile() to do that. Like this.

CFileFind fileName;

static const TCHAR UseFile[] = _T("F:\\Resources\\Files\\ReadFile.txt");

BOOL ress = fileName.FindFile(UseFile);
fileName.FindNextFile();
if(ress)
{
		AfxMessageBox((LPCSTR)fileName.GetFileTitle(), MB_OK);
}

I want to use a CString here, because file path is on CString format, which I have already found. Can you give me a clue how to use it here in this code.

That mean I have to cast CString to a char buffer, is that right? Is it possible?

eranga262154 22 Junior Poster

Great.......