|
这个问题比较可笑吧。
可是我就是搞不定,
现在网上down下了一个例子程序,是读出SD卡的一个test.txt文件。
- BOOL CSDMMCDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // 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
-
- CenterWindow(GetDesktopWindow()); // center to the hpc screen
- // TODO: Add extra initialization here
- [color=#FF0000]m_strFileName = "test.txt"; [/color] // 文件名
- UpdateData(FALSE);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- // "创建文件/打开文件" 按键单击事件代码
- void CSDMMCDlg::OnCreateFile()
- {
- CString filename = "";
- UpdateData(TRUE);
- if (m_strFileName == "")
- {
- MessageBox(_T("请输入文件名!"));
- return;
- }
-
- // 取得文件名及路径
- filename = _T("\\Storage Card\") + m_strFileName;
- // 创建一个文件或打开一个文件
- hFile = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0,
- NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- MessageBox(_T("在SD/MMC卡上创建文件失败!"));
- return;
- }
- MessageBox(_T("成功创建/打开文件:") + filename);
- }
- // "读" 按键单击事件代码
- void CSDMMCDlg::OnReadfile()
- {
- DWORD filelen,actlen;
- char *pcharbuff;
- if (hFile == INVALID_HANDLE_VALUE)
- {
- MessageBox(_T("文件未打开!"));
- return;
- }
-
- filelen = GetFileSize(hFile, NULL); /* 获取文件大小 */
- if (filelen == 0xFFFFFFFF)//文件为什么不能是4G?
- {
- MessageBox(_T("获取文件大小失败!"));
- return;
- }
- BOOL ret = SetFilePointer(hFile, 0, NULL, FILE_BEGIN); /* 移动文件指针到文件开头 */
- if (ret == 0xFFFFFFFF)
- {
- MessageBox(_T("将文件指针移至文件开头失败!"));
- return;
- }
- pcharbuff = new char[filelen];
- ret = ReadFile(hFile, pcharbuff, filelen, &actlen, NULL); /* 从文件中读出数据 */
- if (ret == TRUE)
- {
- LPTSTR pStr = m_strDisp.GetBuffer(filelen);
- // 将字节转化为 Unicode 字符串
- MultiByteToWideChar(CP_ACP, 0, pcharbuff, filelen, pStr, filelen);
- m_strDisp.ReleaseBuffer();
- UpdateData(FALSE); /* 将读出的数据显示出来 */
- MessageBox(_T("读文件成功!"));
- }
- else
- {
- UpdateData(FALSE);
- MessageBox(_T("读文件失败!"));
- }
- if (pcharbuff != NULL)
- delete[] pcharbuff;
- }
复制代码
读txt等小文件是没有问题,但是当我把文件换成NK.nb0(30M)就读取失败了,怎么回事?
是不是new不能分配那么大的内存?
如果确实是这个原因,那应该怎么样做才能读出这么大的文件呢?
谢谢。
|
|