CFile not working properly, can't figure out why (last message not sent properly) 
Author Message
 CFile not working properly, can't figure out why (last message not sent properly)

Im having troubles with CFile.  I know I'm probably doing something wrong,
and it will be painfully obvious but I can't seem to find the problem.  I
have a Win32 App that requires the prescence of certain files.  I've added
these to the program's resources, so the program will create the files if
they are not present.  However I can't seem to make the program open new
files for writing.  I've simplified the non-working code as much as I can
while still keeping the broken code, and put it in and MFC compatible
ConsoleApplication.  I'm using VC++6.0.  Here's my code, along with a
GetFileException function that returns a pointer to a string with the error
in plain text.

Thanks for the help.
Cam Upright

// test.cpp : Defines the entry point for the console application.

#include "StdAfx.h"
#include "test.h"
#include <windows.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

////////////////////////////////////////////////////////////////////////////
/
// The one and only application object

CWinApp theApp;

using namespace std;

char    OutputString[64];

char* GetFileException (CFileException &error)
{
 switch(error.m_cause)
 {
  case CFileException::none :
   strcpy(OutputString, "CFileException::none");
   break;
  case CFileException::generic :
   strcpy(OutputString, "CFileException::generic");
   break;
  case CFileException::fileNotFound :
   strcpy(OutputString, "CFileException::fileNotFound");
   break;
  case CFileException::badPath :
   strcpy(OutputString, "CFileException::badPath");
   break;
  case CFileException::tooManyOpenFiles :
   strcpy(OutputString, "CFileException::tooManyOpenFiles");
   break;
  case CFileException::accessDenied :
   strcpy(OutputString, "CFileException::accessDenied");
   break;
  case CFileException::invalidFile :
   strcpy(OutputString, "CFileException::invalidFile");
   break;
  case CFileException::removeCurrentDir :
   strcpy(OutputString, "CFileException::removeCurrentDir");
   break;
  case CFileException::directoryFull :
   strcpy(OutputString, "CFileException::directoryFull");
   break;
  case CFileException::badSeek :
   strcpy(OutputString, "CFileException::badSeek");
   break;
  case CFileException::hardIO :
   strcpy(OutputString, "CFileException::hardIO");
   break;
  case CFileException::sharingViolation :
   strcpy(OutputString, "CFileException::sharingViolation");
   break;
  case CFileException::lockViolation :
   strcpy(OutputString, "CFileException::lockViolation");
   break;
  case CFileException::diskFull :
   strcpy(OutputString, "CFileException::diskFull");
   break;
  case CFileException::endOfFile :
   strcpy(OutputString, "CFileException::endOfFile");
   break;
 }

 strcat(OutputString, "\n");
 return OutputString;

Quote:
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
 int nRetCode = 0;
 CFile     fDefaultMake;
 CFileException   error;

 // initialize MFC and print and error on failure
 if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
 {
  // TODO: change error code to suit your needs
  cerr << _T("Fatal Error: MFC initialization failed") << endl;
  return 1;
 }

 // TODO: code your application's behavior here.
 CString strHello;
 strHello.LoadString(IDS_HELLO);
 cout << (LPCTSTR)strHello << endl;

 // MY code
 if (!fDefaultMake.Open("c:\\test.txt", CFile::modeCreate ||
CFile::modeWrite, &error))
 {
  printf("Couldn't make new file\n");
  printf("%s\n", GetFileException(error));
  return FALSE;
 }

 return TRUE;

Quote:
}



Fri, 21 Dec 2001 03:00:00 GMT  
 CFile not working properly, can't figure out why (last message not sent properly)


Quote:
>  // MY code
>  if (!fDefaultMake.Open("c:\\test.txt", CFile::modeCreate ||

If you've copied this exactly as you've got it, then there's your problem
^^^.
You want a bitwise OR (|)..


Fri, 21 Dec 2001 03:00:00 GMT  
 CFile not working properly, can't figure out why (last message not sent properly)
...
Quote:
> However I can't seem to make the program open new
>files for writing.  I've simplified the non-working code as

much as I can

Didn't try your code, but the open looks correct. Is it
possible that the file has it's read only attribute set in
the operating system?

Good Luck,
Thomas Miles



Fri, 21 Dec 2001 03:00:00 GMT  
 CFile not working properly, can't figure out why (last message not sent properly)
On Mon, 05 Jul 1999 16:09:27 GMT, "Cam Upright"

Quote:

> if (!fDefaultMake.Open("c:\\test.txt", CFile::modeCreate ||
>CFile::modeWrite, &error))

Use | rather than ||

Steve
--
Steve Wolstenholme
Neural Network Software for Windows  
http://www.tropheus.demon.co.uk



Fri, 21 Dec 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. CFile not working properly, can't figure out why

2. AtlReportError not working in ATL 7.0 (Injected code doesn't handle ErrorInfo properly)

3. TCP: Why should send + closesocket sometimes not work?

4. Don't know why resourses not working?

5. why doesn't atof work properly ?

6. Why doesn't Pointer Return work properly?

7. why vc5.0 can not be properly run under some win98

8. Why my font is not properly display?

9. why vc5.0 can not be properly run under some win98

10. Tab key to tab between controls does not work properly

11. Scroll bars not working properly

12. Links not working properly in the Object Browser

 

 
Powered by phpBB® Forum Software