hello I'm new to this and will like some help. I will like to create a visual program with to command buttons, the first to open a file by having the user choose the file and the second command button is to open the file and read in the text. here is what I have which gives 2 errors and a warning:

void CUltraReadDlg::OnOpen() 
{
    //MY CODE STARTS HERE

    typedef struct tagOFN { //ofn
        LPCTSTR lpstrFilter;
        LPCTSTR lpstrInitialDir;
    } OPENFILENAME;

    CFileDialog m_ldFile(TRUE);

    // Initialize the starting directory

    m_ldFile.m_ofn.lpstrInitialDir="C:\\Temp\\";

    // Show the file open dialog and capture the result

    if (m_ldFile.DoModal() == IDOK)
    {   m_sResults = m_ldFile.GetPathName() + m_ldFile.GetFileName();
        UpdateData(FALSE);
    }

void CUltraReadDlg::OnRun() 
    // //MY CODE STARTS HERE
#include <fstream>
#include <iostream>
#include <string>         
using namespace std;

int main()
{ 
    ifstream m_fileRead(m_sResults);
    if (m_fileRead.fail()) {
        cerr << "unable to open file for reading." <<endl;
        exit(1);
    }

    string nextToken;
    while (inFile >> nextToken){
        cout << "Token: " << nexttoken << endl;


    }

    m_fileRead.close();
    return 0;

}

    //END OF MY CODE
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Attachments myprog.jpg 20.5 KB

here are the errors

Compiling...
UltraReadDlg.cpp
c:\program files\microsoft visual studio\vc98\include\errno.h(29) : warning C4518: 'extern ' : storage-class or type specifier(s) unexpected here; ignored
c:\program files\microsoft visual studio\vc98\include\errno.h(29) : error C2143: syntax error : missing ';' before 'string'
c:\program files\microsoft visual studio\vc98\include\errno.h(29) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

UltraRead.exe - 2 error(s), 1 warning(s)

Those errors seem to be coming from Microsoft include files. I suspect its caused by something in your own program. Post the complete UltraReadDlg.cpp file, or at least the first 20-30 lines that show all the includes. You might also have to inspect the contents of stdafx.h file, if you have changed it since the compiler originally generated it.

The structure of your code is like this:

void CUltraReadDlg::OnOpen()
{
	// ...
	void CUltraReadDlg::OnRun()

#include <fstream>
#include <iostream>
#include <string>
	using namespace std;

	int main()
	{
		//...
	}
}

How could that ever compile?

Those errors seem to be coming from Microsoft include files. I suspect its caused by something in your own program. Post the complete UltraReadDlg.cpp file, or at least the first 20-30 lines that show all the includes. You might also have to inspect the contents of stdafx.h file, if you have changed it since the compiler originally generated it.

Here is the file

Attachments
// UltraRead.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "UltraRead.h"
#include "UltraReadDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CUltraReadApp

BEGIN_MESSAGE_MAP(CUltraReadApp, CWinApp)
	//{{AFX_MSG_MAP(CUltraReadApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUltraReadApp construction

CUltraReadApp::CUltraReadApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CUltraReadApp object

CUltraReadApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CUltraReadApp initialization

BOOL CUltraReadApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	CUltraReadDlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}
This article has been dead for over six months. Start a new discussion instead.