Click to See Complete Forum and Search --> : Reading into LPCTSTR


GigaRoc
November 21st, 2005, 05:55 PM
i've never used LPCTSTR before, but the class to output to an LCD device requires it....

that being said, how would it be posable to read in char by char from a file and put it in a stack of type LPCTSTR?

i've tried several Methods with little to no success. Please Help

Thanks
GigaRoc

Siddhartha
November 21st, 2005, 06:09 PM
A LPCTSTR is a user-defined type (using typedef) which is a constant character string.

Simply said, it is -
typedef const TCHAR * LPCTSTR; TCHAR takes form char for a non-UNICODE build and form wchar_t for UNICODE builds.
Hence, LPCTSTR for non-UNICODE builds is -
typedef const char* LPCTSTR; And, for UNICODE builds is -
typedef const wchar_t* LPCTSTR; ...To put it simply.

It takes either of the forms above depending on your definition of the UNICODE macro in the pre-processor options.

So, coming to the problem at hand... If a function accept a LPCTSTR, it is simply expecting a constant string pointer, and you need to pass the string to it.

Sample -// Pass a LPCTSTR to LoadLibrary API
LPCTSTR pszMyDllName = _T ("MyDll.dll");

HMODULE hMyDllModule = LoadLibrary (pszMyDllName); And... i've tried several Methods with little to no success. Understand the above, and if things don't work, post the latest one... ;)

GigaRoc
November 21st, 2005, 06:57 PM
i've done that earlyer in the program, with success. this is the code for my function:

void read_from_file( stack<LPCTSTR>* myStack, int& numLinesRead)
{
//reads from file inputs into stack
//DOES NOT WORK!
//i'm working on this, really

LPCTSTR string;
char C;
FILE * pFile;
int curChar;

//open file
pFile = fopen ("myfile.txt","r");

if (pFile==NULL) perror ("Error opening file");
else
{
while (C != EOF)
{
curChar=0
C=fgetc(pFile);
curChar++;
while ((C != '\n')&& (C != EOF))
{
if (curChar <= MAX_CHAR)
{
string=string+C;
}
else
{
myStack.push(string);
string=_T(' ');
curChar=1;
}
C= fgetc (pFile);
curChar++;
}
myStack.push(string);
}
}

fclose(pFile);
return;
}

the problem is, i need to typecase a char or char* to LPCTSTR

unless i'm doing this in some off-the-wall totaly ineffection, or uncommon way.

P.S. i've never formaly learned C++ only what i've read, or seen in other peoples code.

Siddhartha
November 22nd, 2005, 04:03 AM
At first look the problems I see are -
void read_from_file( stack<LPCTSTR>* myStack, int& numLinesRead)
{
//reads from file inputs into stack
//DOES NOT WORK!
//i'm working on this, really

LPCTSTR string; // Non initialized pointer contains junk value. Assign a constant string
char C;
FILE * pFile;
int curChar;

//open file
pFile = fopen ("myfile.txt","r");

if (pFile==NULL) perror ("Error opening file");
else
{
while (C != EOF)
{
curChar=0
C=fgetc(pFile);
curChar++;
while ((C != '\n')&& (C != EOF))
{
if (curChar <= MAX_CHAR)
{
string=string+C; // What does string contain that you are adding C to it? It contains junk
}
else
{
myStack.push(string); // Pushing Junk... ;-)
string=_T(' ');
curChar=1;
}
C= fgetc (pFile);
curChar++;
}
myStack.push(string);
}
}

fclose(pFile);
return;
}

the problem is, i need to typecase a char or char* to LPCTSTRWhere do you need to do this?

Also, why are you adding a character to the constant string pointer? Constant means cannot be changed. Anyways, a string is a pointer to a location in memory - adding a character doesn't make sense.

Perhaps, you need a stack of a LPSTR, and not LPCTSTR. Perhaps, you just need a stack of characters.

P.S. i've never formaly learned C++ only what i've read, or seen in other peoples code. Pick a good book, and read it... Random attempts won't help you.