CStdioFile

CStdioFile is a wrapper on CFile with additional methods. This gives the facility to read a single line of string from a text file. It also has write function to write a line of string to a file.

String read/write

CStdioFile::ReadString - Reads a single line of text and put in the CString argument passed as reference.

CStdioFile::WriteString Writes a single line of string to a file and adds a newline character at the end.

ReadString example

#include "stdafx.h"

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
  CStdioFile textFile;
  CString strLine;
  
  /*initialize MFC and print and error on failure */
  if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  {
    cerr << _T("Fatal Error: MFC initialization failed") << endl;
    return 1;
  }
  if (argc < 2)
  {
    cerr << "Give a text file as input";
    retun -1;
  }
  /* Open a text file as given in argument */
  if(textFile.Open(argv[1], CFile::modeRead|CFile::typeText) != TRUE)
  {
    cout << "cannot open file ";
    return -1;
  }
  /*Read one line at a time and print in console */
  while(textFile.ReadString(strLine) != NULL)
  {
    cout << (LPCTSTR)strLine << endl;
  }
  textFile.Close();
  return 0;
}

ReadString output

We are printing our own program source file using our executable.

C:\>StdioFileDemo.exe StdioFileDemo.cpp

#include "stdafx.h"

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
  CStdioFile textFile;
  CString strLine;
  
  /*initialize MFC and print and error on failure */
  if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  {
    cerr << _T("Fatal Error: MFC initialization failed") << endl;
    return 1;
  }
  if (argc < 2)
  {
    cerr << "Give a text file as input";
    retun -1;
  }
  /* Open a text file as given in argument */
  if(textFile.Open(argv[1], CFile::modeRead|CFile::typeText) != TRUE)
  {
    cout << "cannot open file ";
    return -1;
  }
  /*Read one line at a time and print in console */
  while(textFile.ReadString(strLine) != NULL)
  {
    cout << (LPCTSTR)strLine << endl;
  }
  textFile.Close();
  return 0;
}

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#