Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Some problems with CStatic

Status
Not open for further replies.

DarkCount

Programmer
Jan 3, 2006
6
RU
Hi, people!
I have some problems with reflection a text on CStatic. I read the first line of a text file with this function:
Code:
char * o_file(const char *filename)
{
		FILE *f=fopen(filename, "r");
		if (f!=0)
			{
			char ch;
			char *result=new char[];
			int i=0;
			fseek(f, 0, SEEK_SET);
		
	while (!feof(f) && ch != '\n' &&  ch != \r')
				{
				ch = fgetc(f);
				if ( ch != '\n' &&  ch != '\r' && !feof(f)) result[i]=ch;
				CSize sz = dc.GetTextExtent(result,strlen(result));
				i++;
				while (sz.cx>=rcstat.right-rcstat.left)
				{
					rcstat.right++;
				};
			fclose(f);
			return(result);
			}
		else 
		{
			fclose(f);
			AfxMessageBox("Error: File not found",MB_OK | MB_ICONSTOP,0);
			return NULL;
		};

};
It runs in this way:
Code:
	if (MyStatic!=NULL) MyStatic->Create(o_file(filename),WS_CHILD|WS_VISIBLE|SS_CENTER|SS_CENTERIMAGE,
		rcstat,this);
When program is running it sometimes works wrong. Why?
 
Code:
char *result=new char[];
I can't believe the compiler doesn't complain about this... You didn't specify how many chars you want to allocate.

Code:
dc.GetTextExtent
I have no idea what dc is? I don't see it declared anywhere, so I don't know what you're trying to do here.

You should use ifstream and string instead of those nasty old C functions like fopen(), fgets()... You can read the line from the file into a string in a few lines like this:
Code:
string strLine;
ifstream inFile( filename );

if ( inFile )
{
    inFile.getline( strLine );
}
else
{
    AfxMessageBox( "Error: File not found", MB_OK | MB_ICONSTOP, 0 );
}
 
I have no idea what dc is?
Sorry, it's misprint (from other function... :)
Code:
    inFile.getline( strLine );
Unfortunately, this function unable to work :( Also it not works in the following aspect:
Code:
     std::getline(inFile, strLine);
Appear errors:
error C2039: 'getline' : is not a member of 'std'
error C2065: 'getline' : undeclared identifier

 
Code:
#include <fstream>  // For ifstream.
#include <string>   // For string.

using namespace std;  // Now you don't need std:: infront of things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top