Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
can any one tell me how to call this logo file into another function its urgent

XML
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}




Thank you:rose::rose:
Posted
Updated 30-Sep-10 1:48am
v4

You may use a ofstream object (instead of std::cout) to direct the output you need to a log file.
For hints on how to write log files, you may read some of the many, many articles about writing log files, here at CodeProject.


[added]

You usually write a log file programmatically, have a look at the "Input/Output with files"[^] at cplusplus.com, it shows you how to write strings (i.e. your log info) to a text file.
[/added]



[added no.2]
Aarti2010 wrote:
myfile.open ("example.txt");

Here you may specify the path of your log file, for instance
myfile.open ("D:\\Documents\\MyLog.txt");

to specify D:\Documents\MyLog.txt path.

As about "understand in one day", take your time: mastering file I/O is a quite rewarding skill.
[/added no.2]



:)



 
Share this answer
 
v6
Comments
aayu 30-Sep-10 6:27am    
hey thanks for this. but can you just tell me one thing where we have to write the code and how we can implement the code.
i have never work on log file so please help.
aayu 30-Sep-10 6:32am    
do i have to write log file in notepad OR i can write directly in visual C++
aayu 30-Sep-10 6:33am    
and ofstream is giving error in visual c++
aayu 30-Sep-10 6:55am    
its very complicated to understand in one day can you please help me with syntax of how to write log file
With Windows is probably better to use the Win32 API for files.
I attach a class for working with files under win32
using the win 32 API. If you read the code you'll understand
how to work with it.

----------- header -------------------
C++
// Standard 32 bits file class

#ifndef DISKFILE_H
#define DISKFILE_H

#if _MSC_VER > 1000
#pragma warning (disable: 4786)
#pragma warning (disable: 4748)
#pragma warning (disable: 4103)
#endif /* _MSC_VER > 1000 */

#include <afx.h>
#include <afxwin.h>

// file defines
#define  FILE_WIN_LINE_END      0x00000000
#define  FILE_UNIX_LINE_END     0x00000001
#define  FILE_DEF_MEM_SIZE      0x000FFFFF
#define  FILE_DEF_MEM_ALL_SIZE  0x0000FFFF
#define  FILE_DEF_LINE_SIZE     0x00001000
#define  FILE_DEF_PATH_SIZE     MAX_PATH
#define  FILE_ATTRIBUTE_FAIL    0xFFFFFFFF
#define  FILE_DEF_END_PTR       0xFFFFFFFF

// pointer move enum
#define  CODE_FILE_MOVETO       0x0001
#define  CODE_FILE_RESETP       0x0002
#define  CODE_FILE_END          0x0003
// error codes
#define  DISKFILE32_ERRCODE_OK                  0
#define  DISKFILE32_ERRCODE_UNKNOWNERROR        1
#define  DISKFILE32_ERRCODE_NOFILEPATH          2
#define  DISKFILE32_ERRCODE_FILEIO              3
#define  DISKFILE32_ERRCODE_DELETEFILE          4
#define  DISKFILE32_ERRCODE_EOF                 5
#define  DISKFILE32_ERRCODE_APPENDTODISK_OPEN   6
#define  DISKFILE32_ERRCODE_APPENDTODISK_WRITE  7
#define  DISKFILE32_ERRCODE_GETFILESIZE_OPEN    8
#define  DISKFILE32_ERRCODE_GETFILESIZE_DO      9
#define  DISKFILE32_ERRCODE_READ_OPEN           10
#define  DISKFILE32_ERRCODE_READ_SETPOINTER     11
#define  DISKFILE32_ERRCODE_READ_DO             12
#define  DISKFILE32_ERRCODE_WRITE_OPEN          13
#define  DISKFILE32_ERRCODE_WRITE_SETPOINTER    14
#define  DISKFILE32_ERRCODE_WRITE_DO            15
#define  DISKFILE32_ERRCODE_APPEND_OPEN         16
#define  DISKFILE32_ERRCODE_APPEND_WRITE        17
#define  DISKFILE32_ERRCODE_NOBUFFER            18

class CDiskFile32
{
private:
   UINT32 unLastErrorCode;
   bool   bEndOfFile;
   UINT32 unPointer;
   TCHAR  cbFilePath[FILE_DEF_PATH_SIZE];

public:
   CDiskFile32();
   ~CDiskFile32();
   UINT32 GetLastErrorCode(void);

   // low level interface
   void   SetFilePath(TCHAR *psFilePath);
   void   GetFilePath(TCHAR *psFilePath);
   bool   CheckFileExistence(void);
   void   Erase(void);
   UINT32 GetSize(void);
   bool   GetEndOfFile(void);
   void   SetPointer(UINT32 unOffset, UINT32 unCode);
   UINT32 GetPointer(void);
   void   Read  (void *pMemBuffer, UINT32 unBytesTo, UINT32 *punBytesDone);
   void   Write (void *pMemBuffer, UINT32 unBytesTo, UINT32 *punBytesDone);
   void   Append(void *pMemBuffer, UINT32 unBytesTo);
};

#endif  /* ! defined (DISKFILE_H) */


----------- implementation -------------------
C++
#include "StdAfx.h"
#include "DiskFile32.h"

CDiskFile32::CDiskFile32()
{
   unLastErrorCode = DISKFILE32_ERRCODE_OK;
   bEndOfFile = false;
   unPointer = 0;
#if defined (UNICODE)
   FillMemory(cbFilePath, 2*FILE_DEF_PATH_SIZE, 0);
#else
   FillMemory(cbFilePath, FILE_DEF_PATH_SIZE, 0);
#endif
}

CDiskFile32::~CDiskFile32()
{
}

UINT32 CDiskFile32::GetLastErrorCode(void)
{
   return unLastErrorCode;
}

void CDiskFile32::SetFilePath(TCHAR *psFilePath)
{
   _tcsncpy(cbFilePath, psFilePath, FILE_DEF_PATH_SIZE-1);
}

void CDiskFile32::GetFilePath(TCHAR *psFilePath)
{
   _tcsncpy(psFilePath, cbFilePath, FILE_DEF_PATH_SIZE-1);
   *(psFilePath+FILE_DEF_PATH_SIZE-1) = L'\0';
}

bool CDiskFile32::CheckFileExistence(void)
{
   if ( ! _tcslen(cbFilePath) )
   {
      unLastErrorCode = DISKFILE32_ERRCODE_NOFILEPATH;
   }
   return (GetFileAttributes(cbFilePath) != FILE_ATTRIBUTE_FAIL);
}

void CDiskFile32::Erase(void)
{
   unLastErrorCode = DISKFILE32_ERRCODE_UNKNOWNERROR;
   try
   {
      if ( ! _tcslen(cbFilePath) )
      {
         unLastErrorCode = DISKFILE32_ERRCODE_NOFILEPATH;
      }
      if ( ! DeleteFile(cbFilePath) )
      {
         unLastErrorCode = DISKFILE32_ERRCODE_DELETEFILE;
      }
   }
   catch(...)
   {
      unLastErrorCode = DISKFILE32_ERRCODE_DELETEFILE;
   }
   unLastErrorCode = DISKFILE32_ERRCODE_OK;
}

UINT32 CDiskFile32::GetSize(void)
{
   UINT32 unFileSize;
   SECURITY_ATTRIBUTES  SecurityAttributes;
   HANDLE hFile;

   unLastErrorCode = DISKFILE32_ERRCODE_UNKNOWNERROR;

   try
   {
      if ( ! _tcslen(cbFilePath) )
      {
         unLastErrorCode = DISKFILE32_ERRCODE_NOFILEPATH;
      }
      SecurityAttributes.nLength = sizeof(SecurityAttributes);
      SecurityAttributes.lpSecurityDescriptor = NULL;
      SecurityAttributes.bInheritHandle = FALSE;
      // Get file
      hFile = CreateFile(cbFilePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
         &SecurityAttributes, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
      if ( INVALID_HANDLE_VALUE == hFile )
      {
         unLastErrorCode = DISKFILE32_ERRCODE_GETFILESIZE_OPEN;
      }
      // Get size
      unFileSize = (UINT32) GetFileSize(hFile, NULL);
      // Larger than 4Gb
      if ( unFileSize == FILE_DEF_END_PTR )
      {
         unLastErrorCode = DISKFILE32_ERRCODE_GETFILESIZE_DO;
      }

      if ( INVALID_HANDLE_VALUE != hFile )
         CloseHandle(hFile);
   }
   catch(...)
   {
   }
   unLastErrorCode = DISKFILE32_ERRCODE_OK;
   return unFileSize;
}

bool CDiskFile32::GetEndOfFile(void)
{
   return bEndOfFile;
}

void CDiskFile32::SetPointer(UINT32 unOffset, UINT32 unCode)
{
   switch(unCode)
   {
      case CODE_FILE_MOVETO:
         unPointer = unOffset;
         break;
      case CODE_FILE_RESETP:
         unPointer = 0;
         break;
      case CODE_FILE_END:
         unPointer = FILE_DEF_END_PTR;
         break;
   }
   bEndOfFile = false;
}

UINT32 CDiskFile32::GetPointer(void)
{
   return unPointer;
}

void CDiskFile32::Read(void *pMemBuffer, UINT32 unBytesTo, UINT32 *punBytesDone)
{
   DWORD dwBytesTo, dwBytesDone;
   SECURITY_ATTRIBUTES SecurityAttributes;
   HANDLE hFile;
   UINT32 unFileSize;
   BOOL bSucceded;

   unLastErrorCode = DISKFILE32_ERRCODE_UNKNOWNERROR;

   if ( NULL == pMemBuffer )
   {
      unLastErrorCode = DISKFILE32_ERRCODE_NOBUFFER;
      return;
   }

   try
   {
      if ( ! _tcslen(cbFilePath) )
      {
         unLastErrorCode = DISKFILE32_ERRCODE_NOFILEPATH;
         return;
      }
      SecurityAttributes.nLength = sizeof(SecurityAttributes);
      SecurityAttributes.lpSecurityDescriptor = NULL;
      SecurityAttributes.bInheritHandle = FALSE;
      // open file
      hFile = CreateFile(cbFilePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
         &SecurityAttributes, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
      if ( INVALID_HANDLE_VALUE == hFile )
      {
         unLastErrorCode = DISKFILE32_ERRCODE_READ_OPEN;
         throw;
      }
      // set file pointers
      unFileSize = (UINT32) GetFileSize(hFile, 0);
      if ( (unPointer == FILE_DEF_END_PTR) || (unPointer > unFileSize) )
         unPointer = unFileSize;
      SetFilePointer(hFile, unPointer, 0, FILE_BEGIN);
      // read file content
      bEndOfFile = false;
      dwBytesTo = unBytesTo;
      bSucceded = ReadFile(hFile, (LPVOID) pMemBuffer, dwBytesTo, &dwBytesDone, NULL);
      if ( !bSucceded )
      {
         unLastErrorCode = DISKFILE32_ERRCODE_READ_DO;
         throw;
      }
      // close file
      if( INVALID_HANDLE_VALUE != hFile )
         CloseHandle(hFile);
      unPointer += dwBytesDone;
      if(dwBytesDone != dwBytesTo)
         bEndOfFile = true;
   }
   catch(...)
   {
      if( INVALID_HANDLE_VALUE != hFile )
         CloseHandle(hFile);
      return;
   }
   unLastErrorCode = DISKFILE32_ERRCODE_OK;
   if ( punBytesDone )
      *punBytesDone = (UINT32) dwBytesDone;
}

void CDiskFile32::Write(void *pMemBuffer, UINT32 unBytesTo, UINT32 *punBytesDone)
{
   DWORD dwBytesTo, dwBytesDone;
   SECURITY_ATTRIBUTES SecurityAttributes;
   HANDLE hFile;
   UINT32 unFileSize;

   unLastErrorCode = DISKFILE32_ERRCODE_UNKNOWNERROR;

   if ( NULL == pMemBuffer )
   {
      unLastErrorCode = DISKFILE32_ERRCODE_NOBUFFER;
      return;
   }

   try
   {
      if ( ! _tcslen(cbFilePath) )
      {
         unLastErrorCode = DISKFILE32_ERRCODE_NOFILEPATH;
         return;
      }
      SecurityAttributes.nLength = sizeof(SecurityAttributes);
      SecurityAttributes.lpSecurityDescriptor = NULL;
      SecurityAttributes.bInheritHandle = FALSE;
      // open file
      hFile = CreateFile(cbFilePath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
         &SecurityAttributes, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL);
      if ( INVALID_HANDLE_VALUE == hFile )
      {
         unLastErrorCode = DISKFILE32_ERRCODE_WRITE_OPEN;
         throw;
      }
      // set file pointer
      unFileSize = (UINT32) GetFileSize(hFile, 0);
      if (unPointer == FILE_DEF_END_PTR)
         unPointer = unFileSize;
      if (unPointer > unFileSize)
      {
         unLastErrorCode = DISKFILE32_ERRCODE_WRITE_SETPOINTER;
         throw;
      }
      SetFilePointer(hFile, unPointer, NULL, FILE_BEGIN);
      // write file content
      dwBytesTo = unBytesTo;
      WriteFile(hFile, (LPVOID) pMemBuffer, dwBytesTo, &dwBytesDone, NULL);
      if ( dwBytesDone < dwBytesTo )
      {
         unLastErrorCode = DISKFILE32_ERRCODE_WRITE_DO;
         throw;
      }
      unPointer += dwBytesDone;
      // close file
      if ( INVALID_HANDLE_VALUE != hFile )
         CloseHandle(hFile);
   }
   catch(...)
   {
      if ( INVALID_HANDLE_VALUE != hFile )
         CloseHandle(hFile);
      return;
   }
   unLastErrorCode = DISKFILE32_ERRCODE_OK;
   if ( punBytesDone )
      *punBytesDone = (UINT32) dwBytesDone;
}

void CDiskFile32::Append(void *pMemBuffer, UINT32 unBytesTo)
{
   SECURITY_ATTRIBUTES SecurityAttributes;
   DWORD dwBytesTo, dwBytesDone;
   HANDLE hFile;

   unLastErrorCode = DISKFILE32_ERRCODE_UNKNOWNERROR;

   if ( NULL == pMemBuffer )
   {
      unLastErrorCode = DISKFILE32_ERRCODE_NOBUFFER;
      return;
   }

   try
   {
      if ( ! _tcslen(cbFilePath) )
      {
         unLastErrorCode = DISKFILE32_ERRCODE_NOFILEPATH;
         return;
      }
      SecurityAttributes.nLength = sizeof(SecurityAttributes);
      SecurityAttributes.lpSecurityDescriptor = NULL;
      SecurityAttributes.bInheritHandle = FALSE;
      // Open file
      hFile = CreateFile(cbFilePath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
         &SecurityAttributes, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL);
      if ( INVALID_HANDLE_VALUE == hFile )
      {
         unLastErrorCode = DISKFILE32_ERRCODE_APPEND_OPEN;
         throw;
      }
      // set file pointer
      SetFilePointer(hFile, 0, NULL, FILE_END);
      // write string
      dwBytesTo = unBytesTo;
      WriteFile(hFile, (LPVOID) pMemBuffer, dwBytesTo, &dwBytesDone, 0);
      if ( dwBytesDone != dwBytesTo )
      {
         unLastErrorCode = DISKFILE32_ERRCODE_APPEND_WRITE;
         throw;
      }
      unPointer = (UINT32) GetFileSize(hFile, 0);
      if( INVALID_HANDLE_VALUE != hFile )
         CloseHandle(hFile);
   }
   catch(...)
   {
      if( INVALID_HANDLE_VALUE != hFile )
         CloseHandle(hFile);
      return;
   }

   unLastErrorCode = DISKFILE32_ERRCODE_OK;
}
 
Share this answer
 
v2
Comments
aayu 30-Sep-10 8:34am    
thanks
Dan Neely 30-Sep-10 10:46am    
*THWAPP*

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900