How can I open and read a text file!!!!!!

Im using Palm-size pc 2.01
Why it crashes in the file.open line????? what am I doing wrong????

CString fname=(_T("mysetce.txt"));
CStdioFile file;
            
if (file.Open(fname,
 CFile::shareExclusive |      CFile::modeRead | CFile::typeText ) )
{
                          file.Read(st,50);
file.Close();

}


coldestAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
MoinConnect With a Mentor Commented:
If you are not stuck with CFile you can use the following code fragment:


HANDLE hFile;
hFile = CreateFile(_T("MyFile.txt"), GENERIC_READ, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);  

if (hFile == INVALID_HANDLE_VALUE)
{
   AfxMessageBox((LPCTSTR)"Unable to open file", MB_OK, 0);
}
//else
//      AfxMessageBox((LPCTSTR)"File opened", MB_OK, 0);

And then use this statement to read data:
char ch;
DWORD dwBytesRead;
ReadFile(hFile, &ch, 1, &nBytesRead, NULL) ;

Hope it will work fine.
0
 
inprasCommented:
Hi use this
I think _T macro  is giving U a problem

char* pFileName = "mysetce.txt";
CStdioFile f1;
if( !f1.Open( pFileName, CFile::modeCreate
       | CFile::modeWrite | CFile::typeText ) ) {
   exit( 1 );
}

Hope this help
Regards
0
 
coldestAuthor Commented:
Hi, thanks for your help.

When i used your method, which is:
char* pFileName = "mysetce.txt";

Im getting this error:
'Open' : cannot convert parameter 1 from 'char*' to 'const unsigned short*'

But this below, doesnt give me any error:
CString fname=(_T("mysetce.txt"));

In addition, Im trying to read a file not write to a file.

However, at the point of 'open', i get this 'Assertion failed' message-box which doesnt help me much
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
coldestAuthor Commented:
Windows CE does not support CStdioFile
0
 
coldestAuthor Commented:
But still im having problem even when I use CFile, although not the same problem!

Here is my code:

char pbuf[50];
CFile f;

if( f.Open(L"mysetce.txt",CFile::modeReadWrite|
CFile::shareDenyNone) )  
{
  UINT d= f.Read(pbuf,50);    
  f.Close();
  str = pbuf;
}

The file "mysetce" is a text file and its not empty.
This 'Open' process above goes through with no error and it runs BUT 'pbuf' remains EMPTY !!!!! (it doesn't read into it) WHY?????
0
 
mikeblasCommented:
What's returned in "d"?

..B ekiM
0
 
waseemanisCommented:
How about Rewinding to the beginning, (just a precautionary measure).
Maybe something like
Seek(CFDile::begin, 0 );

I guess it should work then.

Waseem.
0
 
coldestAuthor Commented:
its retuned zero in "d"

dear waseemanis, my palm top is currently away, so Im going to have to wait for another 2 weeks.
I will let you know as soon as I try your test.
Thanks a lot for your help.
0
 
waseemanisCommented:
How about trying on Emulator.
0
 
coldestAuthor Commented:
You cannot test/debug for open/read/write files on the Emulator

So I should wait till I get my palm top back.

Thanks
0
 
waseemanisCommented:
why sure you can...all youy need to do is to empfile to the emulator.
Try it !!
Luck Waseem.
0
 
jgalt00Commented:
Have you tried something like:
TCHAR szName[32] = TEXT("MyFile.TXT");
and then use szName as your file name in the open call?

Jeff.
0
 
waseemanisCommented:
Is there some difference between using CFile and the API MOIN ???
I guess CFile would work perfect.

Luck,
Waseem.
0
 
coldestAuthor Commented:
Thanks alot for all your help.
But currently Im involved in other projects and my palm-top is away.
I will let you know a.s.a I get it back.
I do appriciate your comments and answers.

Melody
0
All Courses

From novice to tech pro — start learning today.