4 Replies - 4076 Views - Last Post: 23 August 2008 - 02:21 PM Rate Topic: -----

#1 Fatal Error  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-August 08

Read then Display .txt file win32

Post icon  Posted 21 August 2008 - 02:32 AM

Ok, so this is pretty much the reason I registered for this forum. I'm sure this has been asked over and over, but the forum search is acting up and just giving me error messages... so I'll make yet another post on the subject.

Out of bordom, and for the experience, I decided to code up a win32 application that would be similar to windows notepad. I've coded some simple win32 things before, but this was the first big challenge I've given myself!

Now, as lame as it is, I was quite proud to make up my own menus using rc files instead of making them on the fly, and learning how to use the standard Open Save dialouges that windows provides, but here's my problem:

I'm having the darndest time trying to make it open a .txt file, and paint it to the window. I'm not even up to the point where I want to edit it, I just want to get it to display (I'll work on edit features later) I know cout and cin don't work out side of the cmd windows... so that's out... I'm guessing strings is the way to go from what I've read? and maybe an array to read the text file into, then make that into a string and display?

sorry for kind of rambling... but I'm a little frustrated... and everything I find seems to talk about cin cout... (perhaps I'm googling the wrong terms?) Probably doesn't help that I've gotten 6 hours of sleep over the last 3 days...

Even a shove in the right direction or to a site that has a good break down and overview of this process would be appreciated.


LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	LPSTR szMessage = "new text";	
	
	switch (message)
	{
		case WM_COMMAND:
			 switch(LOWORD(wParam))
			 {
								   case ID_FILE_OPEN:
										{
											OPENFILENAME ofn;
											char szFileName[MAX_PATH] = "";

											ZeroMemory(&ofn, sizeof(ofn));

											ofn.lStructSize = sizeof(ofn); //2000 and XP only
											ofn.hwndOwner = hwnd;
											ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
											ofn.lpstrFile = szFileName;
											ofn.nMaxFile = MAX_PATH;
											ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
											ofn.lpstrDefExt = "txt";

											if(GetOpenFileName(&ofn))
											{														 //read the file line by line
												char buffer[256];
												ifstream myfile (szFileName);
												
												while (! myfile.eof() )
												{
													  myfile.getline (buffer,100);
													  cout << buffer << endl;
												}
												return 0;
											}  
												 
											} 
											
								   break;
								   case ID_FILE_SAVE:
										{
											OPENFILENAME ofn;
											char szFileName[MAX_PATH] = "";

											ZeroMemory(&ofn, sizeof(ofn));

											ofn.lStructSize = sizeof(ofn); // 2000 and XP only
											ofn.hwndOwner = hwnd;
											ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
											ofn.lpstrFile = szFileName;
											ofn.nMaxFile = MAX_PATH;
											ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
											ofn.lpstrDefExt = "txt";

											if(GetSaveFileName(&ofn))
												  {
												  // Do something usefull with the filename stored in szFileName 
												  }
										}
								   break;
								   case ID_FILE_EXIT:
										PostMessage(hwnd, WM_CLOSE, 0, 0);
								   break;
								   }
								   
						 
		case WM_PAINT:
			 hdc = BeginPaint(hwnd, &ps);
			 TextOut(hdc, 20, 20 , szMessage, strlen(szMessage));
			 EndPaint(hwnd, &ps);
		break;
		case WM_CLOSE:
			 DestroyWindow(hwnd);
		break;
		case WM_DESTROY:
			PostQuitMessage (0);	   /* send a WM_QUIT to the message queue */
			break;
		default:					  /* for messages that we don't deal with */
			return DefWindowProc (hwnd, message, wParam, lParam);
	}

	return 0;
}



//yes... I do know that my code as it stands doesn't work... it is ... Frankenstien, parts from
//different things I've coded in the past, which I havent documented very well... to cut down the
//size of this post I've removed all the data pertaining to window generation...



Is This A Good Question/Topic? 0
  • +

Replies To: Read then Display .txt file win32

#2 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,901
  • Joined: 10-May 07

Re: Read then Display .txt file win32

Posted 21 August 2008 - 02:36 AM

View PostFatal Error, on 21 Aug, 2008 - 05:32 AM, said:

I'm having the darndest time trying to make it open a .txt file, and paint it to the window.

From what I remember of similar projects, you'll want to load the rich edit common dialog. From there you'll use wspaint messages to fill the rich edit with the content from the text file. Sorry I don't have more to offer, as this was about 4 years ago when I worked on just projects.

Best of luck to you!

I'm sure someone else will be able to help you out in much more detail.
Was This Post Helpful? 0
  • +
  • -

#3 Fatal Error  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-August 08

Re: Read then Display .txt file win32

Posted 21 August 2008 - 07:58 PM

hmmm... that provided some usefull information, but I still dont understand how to get it to work... any examples I can look at?

EDIT:

ok, so I think that
if(GetOpenFileName(&ofn))
{								  //where we would start if cmd window
	ifstream in;
	int num;
	while(!in.eof())
	{
		 in >> num;
		 cout << num << endl;
	}
}



so, if this was a dos based program, that *should* display the file's contents to the screen (correct me if I'm wrong)
But because I'm making a win32 application, cout doesn't work, do I have to write that to szMessage? (see code from OP)
I'm still confused on this subject, and I cant seem to find a walk through of this for anything other than a cmd window application.

In the meantime, I'll keep looking around, maybe I'll find something somewhere.

This post has been edited by Fatal Error: 22 August 2008 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#4 dwayne  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 02-August 08

Re: Read then Display .txt file win32

Posted 22 August 2008 - 06:29 PM

Perhaps find an open-source program to do such a thing, look at it, and make your own code. Just remember to give credit where credit is due.

I believe the easiest way would be to read the file into an internal buffer, write the text to an editable field, then when saved, take the text from that editable field, dump it to the buffer, and write it all back into the file.

Hope that helped/made sense.
Was This Post Helpful? 0
  • +
  • -

#5 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Read then Display .txt file win32

Posted 23 August 2008 - 02:21 PM

You can use a standard edit control for your display (this is what Notepad actually does).

Without writing this for you, the basic steps are:


*) Create your window class and register it.

*) Create an instance of your window with CreateWindow or similar function.

*) In your message handler, create code that will handle the WM_CREATE message. From there, when receiving this message, create an edit control with the CreateWindow call. You can create an edit control by specifying "EDIT" as
the class name. Make sure that you set the style that makes the edit control Multi line.

*) Create a function that sizes the edit control to whatever the window size is. You'll need the functions GetClientRect and SetWindowPos, as well as possibly others.

*) For your code that handles WM_CREATE, add a call to the function previously mentioned, to set the initial window size.

*) In your message handler, create code to handle WM_SIZE, which should also call the aforementioned function.

*) To get and set the contents of the buffer, use the WM_GETTEXT and WM_SETTEXT messages. Use the SendMessage function to do this. For example, to set the text to "Hello World", you'd have:

LPCTSTR text = _T("Hello World");
SendMessage(hEditCtrl, WM_SETTEXT, 0, (LPARAM)text);

Use the WM_GETTEXTLENGTH to get the text length before actually attempting to get the text (so you can allocate the appropriate buffer).

This is a very basic overview of some of the steps needed to make a text editor like Notepad.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1