| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2912 人关注过本帖
标题:用CFile 打开文件出错,求助
只看楼主 加入收藏
弱智抓蛙男
Rank: 1
等 级:新手上路
帖 子:264
专家分:0
注 册:2006-11-24
收藏
 问题点数:0 回复次数:2 
用CFile 打开文件出错,求助

//代码片段如下:
//打开文件
bool File_manager::Open_file(CString path)
{
CFile info_file;
CString str_buffer;
CFileException cfe;
//AfxMessageBox(path);
if(info_file.Open(path,CFile::shareDenyNone,&cfe) == 0)
{
return FALSE;
}
CArchive ar(&info_file,CArchive::load);
ar>>str_buffer;
//info_file.Read(str_buffer,2);
AfxMessageBox(str_buffer);
ar.Close();
info_file.Close();
return TRUE;
}
第一次用vc++.net打开文件,水平有限,
编译通过,打开某个txt文件时出错信息为:

“试图越过其尾端对一个未知名的文件进行读写”

2个都是txt文件,一个能读,一个要出这个错,不知道为什么,请高手解答,
不甚感激!!

搜索更多相关主题的帖子: CFile 文件 
2006-12-17 11:45
弱智抓蛙男
Rank: 1
等 级:新手上路
帖 子:264
专家分:0
注 册:2006-11-24
收藏
得分:0 
2个txt文件,一个内容简单:
sdfasdfasdfasdfsadf
dsfasdfasdfasd
sadfasdfasdf
2006-12-17 11:48
弱智抓蛙男
Rank: 1
等 级:新手上路
帖 子:264
专家分:0
注 册:2006-11-24
收藏
得分:0 

另一个大一点:
http://www.codeproject.com/listctrl/
http://www.vckbase.com/document/listdoc.asp?mclsid=3&sclsid=323

void CChildFrame::OnFileOpen()
{
// szFilters is a text string that includes two file name filters:
// "*.my" for "MyType Files" and "*.*' for "All Files."
char CChildFrame::szFilters[]=
"MyType Files (*.my)|*.my|All Files (*.*)|*.*||";

// Create an Open dialog; the default file name extension is ".my".
CFileDialog fileDlg (TRUE, "my", "*.my",
OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);

// Display the file dialog. When user clicks OK, fileDlg.DoModal()
// returns IDOK.
if( fileDlg.DoModal ()==IDOK )
{
CString pathName = fileDlg.GetPathName();

// Implement opening and reading file in here.
...
//Change the window's title to the opened file's title.
CString fileName = fileDlg.GetFileTitle ();

SetWindowText(fileName);
}

}

All items in a list view control must have the same number of subitems.

Class CListCtrl provides several functions for inserting, deleting, finding, and modifying these items. For more information, see CListCtrl::GetItem, CListCtrl::InsertItem, and CListCtrl::FindItem, Using CListCtrl: Adding Items to the Control, and Using CListCtrl: Scrolling, Arranging, Sorting, and Finding in List Controls.


VC中ctrllist的使用与数据库链接

清浪冲天 马敬发 QQ: 283989349 E-mail: mjf150080@tom.com

有问题时,我们可以通过http://www.hustc.mpc.cn 这个论坛,

或 通过QQ 共同学习和讨论


// DIALOG_TRAINCOURSE.cpp : implementation file
//

#include stdafx.h
#include train.h
#include DIALOG_TRAINCOURSE.h
#include CourseInfo.h
#include DIALOG_APPISE.h
#include CourseSet.h

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

/////////////////////////////////////////////////////////////////////////////
// CDIALOG_TRAINCOURSE dialog


CDIALOG_TRAINCOURSE::CDIALOG_TRAINCOURSE(CWnd* pParent /*=NULL*/)
: CDialog(CDIALOG_TRAINCOURSE::IDD, pParent)
{
//{{AFX_DATA_INIT(CDIALOG_TRAINCOURSE)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}


void CDIALOG_TRAINCOURSE::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDIALOG_TRAINCOURSE)
DDX_Control(pDX, IDC_LIST1, m_content);
//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDIALOG_TRAINCOURSE, CDialog)
//{{AFX_MSG_MAP(CDIALOG_TRAINCOURSE)
ON_BN_CLICKED(IDC_BUTTONADD, OnButtonadd)
ON_BN_CLICKED(IDC_BUTTONDEL, OnButtondel)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDIALOG_TRAINCOURSE message handlers

void CDIALOG_TRAINCOURSE::OnButtonadd()
{
// TODO: Add your control notification handler code here
m_database.Close();
CCourseInfo m_courseinfo;
m_courseinfo.m_database.Open(_T(train));
m_courseinfo.DoModal();
RefreshData();
}

void CDIALOG_TRAINCOURSE::OnOK()
{
// TODO: Add extra validation here
CDIALOG_APPISE m_apprisemanager;
int i=m_content.GetSelectionMark();
CString strSQL;
if(i==-1){
MessageBox(please choose a course first!);
}
else{
m_apprisemanager.course_id=atoi(m_content.GetItemText(i,0));
m_database.Close();
m_apprisemanager.m_database.Open(_T(train));
m_apprisemanager.DoModal();
}

}

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

// TODO: Add extra initialization here
m_content.InsertColumn(0,课程号);
m_content.InsertColumn(1,课程类别);
m_content.InsertColumn(2,课程中文名称);
m_content.InsertColumn(3,课程英文名称);
m_content.InsertColumn(4,课程描述);
m_content.InsertColumn(5,课程学时);
m_content.InsertColumn(6,教材);
m_content.InsertColumn(7,等效课程);
m_content.InsertColumn(8,预修课程);
m_content.InsertColumn(9,初训/复训标志);
m_content.InsertColumn(10,开课部门);
RECT rect;
m_content.GetWindowRect(&rect);
int wid = rect.right - rect.left;
m_content.SetColumnWidth(0,wid/11);
m_content.SetColumnWidth(1,wid/11);
m_content.SetColumnWidth(2,wid/11);
m_content.SetColumnWidth(3,wid/11);
m_content.SetColumnWidth(4,wid/11);
m_content.SetColumnWidth(5,wid/11);
m_content.SetColumnWidth(6,wid/11);
m_content.SetColumnWidth(7,wid/11);
m_content.SetColumnWidth(8,wid/11);
m_content.SetColumnWidth(9,wid/11);
m_content.SetColumnWidth(10,wid/11);
m_content.SetExtendedStyle(LVS_EX_FULLROWSELECT);
RefreshData();

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

void CDIALOG_TRAINCOURSE::RefreshData()
{
if(!m_database.IsOpen()){
m_database.Open(_T(train));
}
m_content.DeleteAllItems();
CCourseSet m_recordset(&m_database);
CString strSQL;
strSQL.Format(select * from COURSE);
m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL);
CDBVariant varValue;
if(m_recordset.GetRecordCount()!=0) m_recordset.MoveFirst();
char buf[20];
int i=0;
while(!m_recordset.IsEOF())
{
int temp=0;
m_recordset.GetFieldValue(temp,varValue);
sprintf(buf,%d,varValue.m_iVal); m_content.InsertItem(i,buf);
m_recordset.GetFieldValue(1,varValue);
m_content.SetItemText(i,1,varValue.m_pstring->GetBuffer(1));
m_recordset.GetFieldValue(2,varValue);
m_content.SetItemText(i,2,varValue.m_pstring->GetBuffer(1));
m_recordset.GetFieldValue(3,varValue);
m_content.SetItemText(i,3,varValue.m_pstring->GetBuffer(1));
m_recordset.GetFieldValue(4,varValue);
m_content.SetItemText(i,4,varValue.m_pstring->GetBuffer(1));
m_recordset.GetFieldValue(5,varValue);
sprintf(buf,%d,varValue.m_iVal); m_content.SetItemText(i,5,buf);
m_recordset.GetFieldValue(6,varValue);
m_content.SetItemText(i,6,varValue.m_pstring->GetBuffer(1));
m_recordset.GetFieldValue(7,varValue);
sprintf(buf,%d,varValue.m_iVal); m_content.SetItemText(i,7,buf);
m_recordset.GetFieldValue(8,varValue);
sprintf(buf,%d,varValue.m_iVal); m_content.SetItemText(i,8,buf);
m_recordset.GetFieldValue(9,varValue);
sprintf(buf,%d,varValue.m_chVal); m_content.SetItemText(i,9,buf);
m_recordset.GetFieldValue(10,varValue);
sprintf(buf,%d,varValue.m_iVal); m_content.SetItemText(i,10,buf);
m_recordset.MoveNext();
i++;
}
}

void CDIALOG_TRAINCOURSE::OnButtondel()
{
// TODO: Add your control notification handler code here
int i=m_content.GetSelectionMark();
CString strSQL;
if(i==-1){
MessageBox(please choose a record first!);
}
else{
int keyid=atoi(m_content.GetItemText(i,0));
strSQL.Format(delete from COURSE where COURSE_ID=%d,keyid);
m_database.ExecuteSQL(strSQL);
strSQL.Format(delete from COURSE_EVALUATION where COURSE_ID=%d,keyid);
m_database.ExecuteSQL(strSQL);
m_database.Close();
RefreshData();
}
}

void CDIALOG_TRAINCOURSE::OnCancel()
{
// TODO: Add extra cleanup here
m_database.Close();
CDialog::OnCancel();
}

2006-12-17 11:51
快速回复:用CFile 打开文件出错,求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.058825 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved