Thread: [Dev-C++] Reading from text file
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Noorez K. <lor...@ho...> - 2005-07-08 20:37:39
|
I am having trouble with reading from a text file in my program. For
testing, I used notepad and typed and let word-wrap put text on the next
line. I typed a lot into this file. I then used my program to try and open
it but it froze. If I use the enter key to put in the newline then my
program works. Whats wrong with it?
#include "SML.h"
// Function called from WindowProcedure to open text
void OpenText(HWND main, HWND hEdit)
{
ifstream openTextFile; // Handle to file
// Filter files visible in the open dialog box
char* filter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0";
char* inLine = new char[900];
int nLength;
// Init the properties of the open dialog box
getOFNproperties(main, &dialogProperties, currentFileName, filter);
// Show the dialog and get the information
if( !GetOpenFileName(&dialogProperties) )
{
return; // Return if user cancels
}
openTextFile.open(currentFileName);
SetDlgItemText(main, IDC_MAIN_EDIT, ""); // Clear the edit control
int cntr = 1;
// Loop unitl the end of file is reached
while( !openTextFile.eof() )
{
nLength = GetWindowTextLength(GetDlgItem(main, IDC_MAIN_EDIT));
openTextFile.getline(inLine,9000);
// A new line must have a "\r\n" so test for both
if( inLine[ strlen(inLine) -1 ] != '\r' )
{
strcat(inLine,"\r");
}
if( inLine[ strlen(inLine) -1] != '\n' )
{
strcat(inLine,"\n");
}
// And the message onto the edit control
SendMessage( GetDlgItem(main, IDC_MAIN_EDIT), EM_SETSEL,
(WPARAM)nLength,
(LPARAM)nLength );
SendMessage( GetDlgItem(main, IDC_MAIN_EDIT), EM_REPLACESEL,
(WPARAM)FALSE,
(LPARAM)inLine );
delete inLine;
inLine = new char[900];
}
untitled = false;
edited = false;
return;
}
_________________________________________________________________
Designer Mail isn't just fun to send, it's fun to receive. Use special
stationery, fonts and colors.
http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines
Start enjoying all the benefits of MSN® Premium right now and get the
first two months FREE*.
|
|
From: Per W. <pw...@ia...> - 2005-07-09 10:19:32
|
Ever heard anyone say that constant numbers within the code is bad? char* inLine = new char[900]; ... openTextFile.getline(inLine,9000); How do you expect 9000 characters to fit into a 900 char buffer? And more - how to you expect to have the extra room to possibly concatenate "\r\n" to the end of the line? By the way, why do you mix C and C++ code? Why not use the std::string class? /Per W On Fri, 8 Jul 2005, Noorez Kassam wrote: > I am having trouble with reading from a text file in my program. For > testing, I used notepad and typed and let word-wrap put text on the next > line. I typed a lot into this file. I then used my program to try and open > it but it froze. If I use the enter key to put in the newline then my > program works. Whats wrong with it? |
|
From: Robert A. <ral...@gm...> - 2005-07-09 13:58:29
|
Also, note that not all line wraps actually add a newline...sometimes
they ONLY wrap the display and allow the line to remain a long line in
the file. Possibly without a linefeed at all.
-Robert
On 7/8/05, Noorez Kassam <lor...@ho...> wrote:
> I am having trouble with reading from a text file in my program. For
> testing, I used notepad and typed and let word-wrap put text on the next
> line. I typed a lot into this file. I then used my program to try and ope=
n
> it but it froze. If I use the enter key to put in the newline then my
> program works. Whats wrong with it?
>=20
> #include "SML.h"
>=20
> // Function called from WindowProcedure to open text
> void OpenText(HWND main, HWND hEdit)
> {
> ifstream openTextFile; // Handle to file
>=20
> // Filter files visible in the open dialog box
> char* filter =3D "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0";
>=20
> char* inLine =3D new char[900];
>=20
> int nLength;
>=20
> // Init the properties of the open dialog box
> getOFNproperties(main, &dialogProperties, currentFileName, filter);
>=20
> // Show the dialog and get the information
> if( !GetOpenFileName(&dialogProperties) )
> {
> return; // Return if user cancels
> }
>=20
> openTextFile.open(currentFileName);
>=20
> SetDlgItemText(main, IDC_MAIN_EDIT, ""); // Clear the edit control
>=20
> int cntr =3D 1;
>=20
> // Loop unitl the end of file is reached
> while( !openTextFile.eof() )
> {
> nLength =3D GetWindowTextLength(GetDlgItem(main, IDC_MAIN_EDIT));
> openTextFile.getline(inLine,9000);
>=20
> // A new line must have a "\r\n" so test for both
> if( inLine[ strlen(inLine) -1 ] !=3D '\r' )
> {
> strcat(inLine,"\r");
> }
> if( inLine[ strlen(inLine) -1] !=3D '\n' )
> {
> strcat(inLine,"\n");
> }
>=20
> // And the message onto the edit control
> SendMessage( GetDlgItem(main, IDC_MAIN_EDIT), EM_SETSEL,
> (WPARAM)nLength,
> (LPARAM)nLength );
>=20
> SendMessage( GetDlgItem(main, IDC_MAIN_EDIT), EM_REPLACESEL,
> (WPARAM)FALSE,
> (LPARAM)inLine );
> delete inLine;
> inLine =3D new char[900];
> }
>=20
> untitled =3D false;
> edited =3D false;
> return;
> }
>=20
> _________________________________________________________________
> Designer Mail isn't just fun to send, it's fun to receive. Use special
> stationery, fonts and colors.
> http://join.msn.com/?pgmarket=3Den-ca&page=3Dbyoa/prem&xAPID=3D1994&DI=3D=
1034&SU=3Dhttp://hotmail.com/enca&HL=3DMarket_MSNIS_Taglines
> Start enjoying all the benefits of MSN(r) Premium right now and get the
> first two months FREE*.
>=20
>=20
>=20
> -------------------------------------------------------
> This SF.Net email is sponsored by the 'Do More With Dual!' webinar happen=
ing
> July 14 at 8am PDT/11am EDT. We invite you to explore the latest in dual
> core and dual graphics technology at this free one hour event hosted by H=
P,
> AMD, and NVIDIA. To register visit http://www.hp.com/go/dualwebinar
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|