Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error in displayin in MsgBox

Status
Not open for further replies.

balajee

Programmer
Joined
Jun 29, 2000
Messages
134
Location
NZ
Hi

I am trying to read a text file by each character and displaying the character read in a message box. I have included these header files: fstream.h and conio.h.

Following is the code I am using. But when run this code I get error at the line "MessageBox(ch);"

Error:
"Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast"

Code:

{
char ch;
ifstream file;

file.open("test.txt",ios::nocreate);

if(!file)

{

MessageBox("UNABLE TO OPEN FILE!!");

goto end;

}

while(file)

{

file.get(ch);
MessageBox(ch);

}

getch();

end:

file.close();

Thanks,
 
Sounds to me like C++ is interpreting the char variable as a number (byte) and that MessageBox takes a string as in parameter.

I verified that this is so in my environment. Additionally the following code worked for me. I simply defined char to be a pointer intead.



#include "stdafx.h"
#include "windows.h"

int main(int argc, char* argv[])
{
char *ch2;
ch2 = "a";
MessageBox(NULL,ch2,"Title",MB_OK);
return 0;
}


Hope this gets you closer to your answer!
 
Thanks for the reply. I tried your suggestion declaring the line "char ch;" as "char *ch;", but now I get the following error at line "file.get(ch);"

class istream &__thiscall istream::get(char &)' : cannot convert parameter 1 from 'char *' to 'char &'
A reference that is not to 'const' cannot be bound to a non-lvalue

Please help,

Thanks
 
It would help if you could copy all of your code and paste it. That way I can see exactly the message on my machine.

Thanks.
 
Hi
I have created a simple MFC Appwizard(exe) project using the defaults settings. I am attaching the code to the click event of "OK" button.

I am attaching herewith the code which is trying to read and display a text file which has just one line.

Code Starts here:

// TestingFileReadingDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestingFileReading.h"
#include "TestingFileReadingDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestingFileReadingDlg dialog

CTestingFileReadingDlg::CTestingFileReadingDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestingFileReadingDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestingFileReadingDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestingFileReadingDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestingFileReadingDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestingFileReadingDlg, CDialog)
//{{AFX_MSG_MAP(CTestingFileReadingDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestingFileReadingDlg message handlers

BOOL CTestingFileReadingDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

void CTestingFileReadingDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CTestingFileReadingDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTestingFileReadingDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

#include <string.h>
#include <fstream.h>
#include <conio.h>
#include &quot;windows.h&quot;

void CTestingFileReadingDlg::OnOK()
{
char *ch2;

ifstream file;

file.open(&quot;test.txt&quot;,ios::nocreate);

if(!file)

{

MessageBox(&quot;UNABLE TO OPEN FILE!!&quot;);

goto end;

}

while(file)

{

file.get(ch2);

MessageBox(ch2);

}

getch();

end:

file.close();


CDialog::OnOK();
}


Code ends here.

The code after the line &quot;void CTestingFileReadingDlg::OnOK()&quot; is the code I have added including the few header files above this. Rest is created by the wizard.

hope this is makes sense,

thanks
 
I have redone my example using a file for input. Create the file and place in the same directory that you are running the compiled app from.




#include &quot;stdafx.h&quot;
#include &quot;windows.h&quot;
#include <stdio.h>
#include <conio.h>

int main(int argc, char* argv[])
{
char ch[2];
char *chOut;
FILE *fp;
ch[1] = '\0';
if((fp = fopen(&quot;textIn.txt&quot;,&quot;r&quot;)) != NULL)
{

while(fread(&ch,sizeof(char),1,fp)>0)
{
MessageBox(NULL,ch,&quot;Title&quot;,MB_OK);
}

fclose(fp);

}
else
{
MessageBox(NULL,&quot;Could not find file 'textIn.text'!&quot;,&quot;File Not Found&quot;,MB_OK);
getch();
}

return 0;
}



MessageBox takes a null terminated string as its input. So I after reading in a character from a file you must place a terminating '\0' at the end for it to be a valid string.


Hope this helps.
 
Hi

I am sorry I could not understand your suggestion of using pointer variable. Now I have used it correctly and there is no error. But the character displayed has some funny characters attached at the end. How do I get rid off these?

Once again sorry for this,

Thanks
 
OK. Got that. Thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top