EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

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;
10    CString strLine;
11    
12    /*initialize MFC and print and error on failure */
13    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
14    {
15      cerr << _T("Fatal Error: MFC initialization failed") << endl;
16      return 1;
17    }
18    if (argc < 2)
19    {
20      cerr << "Give a text file as input";
21      retun -1;
22    }
23    /* Open a text file as given in argument */
24    if(textFile.Open(argv[1], CFile::modeRead|CFile::typeText) != TRUE)
25    {
26      cout << "cannot open file ";
27      return -1;
28    }
29    /*Read one line at a time and print in console */
30    while(textFile.ReadString(strLine) != NULL)
31    {
32      cout << (LPCTSTR)strLine << endl;
33    }
34    textFile.Close();
35    return 0;
36  }

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;
}

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sat 26 Apr/2014 19:20:17(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.