| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1257 人关注过本帖
标题:指针和数组的问题(文件操作CFile)
只看楼主 加入收藏
shixi2011
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2011-5-12
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:6 
指针和数组的问题(文件操作CFile)
根据孙鑫的vc++写的程序:
程序代码:
char *pBuf;
DWORD dwFileLen;
CFileDialog fileDlg(TRUE);
    fileDlg.m_ofn.lpstrFilter="Text Files(*.doc)\0*.doc\0*.jpg\0All File(*.*)\0*.*\0\0";
    //fileDlg.m_ofn.lpstrDefExt="doc";

    if (IDOK==fileDlg.DoModal())
    {
        CFile file(fileDlg.GetFileName(),CFile::modeRead|CFile::typeBinary);
        //char *pBuf;
        //DWORD dwFileLen;
        dwFileLen=file.GetLength();
        pBuf=new char[dwFileLen+1];
        pBuf[dwFileLen]=0;
        file.Read(pBuf,dwFileLen);
        file.Close();        
    }

然后在调用另存为函数实现文件的保存
程序代码:
CFileDialog fileDlg(FALSE);
    fileDlg.m_ofn.lpstrFilter="Text Files(*.doc)\0*.doc\0All File(*.*)\0*.*\0\0";
    
    if (IDOK==fileDlg.DoModal())
    {
        CFile file(fileDlg.GetFileName(),CFile::modeCreate|CFile::modeWrite);
        file.Write(pBuf,dwFileLen);
        file.Close();
        //MessageBox(pBuf);
    }

这部分功能已经实现
我现在要在此基础上扩展到我自己的应用程序,调用自己的API程序,遇到问题了。
之前我的API程序,都是定义定长的buffer然后调用API函数:xChannelIOWrite(hChannel,uint32_t ulAreaNumber,uint32_t ulOffset,uint32_t ulDataLen,void* pvData, uint32_t ulTimeout);其中 ulDataLen是我定义的buffer的长度void* pvData这个参数就是我定义的数据名,
比如:buffer定义为:unsigned char abWriteIOBuffer[100] = {0};
相应的API函数就是xChannelIOWrite( hChannel,0, 0,100 , abWriteIOBuffer,10)
我现在把两者结合起来时就有问题了,CFile里面buffer是定义的指针形式,我的应用程序是数组的,我把应用程序改了,API调用就返回错误,本人新手指针和数组这块不是很清楚求指教!!
搜索更多相关主题的帖子: file 
2011-12-13 11:05
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:20 
那你怎么调用xChannelIOWrite写文件数据pBuf的呢?
void* pvData要求你传送的就是数据块首地址, char *pBuf本身就是指向文件数据的首地址。

我们都在路上。。。。。
2011-12-13 12:33
shixi2011
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2011-5-12
收藏
得分:0 
回复 2楼 hellovfp
这是完整的MFC的程序
程序代码:
#include "stdafx.h"
#include "document transmission.h"
#include "document transmissionDlg.h"

#include "cifXTest_Console.h"
#include "cifXErrors.h"
#include "cifXUser.h"
#include "rcX_Public.h"
#include "rcX_User.h"
#include "TLR_Diag.h"
#include "TLR_Types.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
  
#define IO_WAIT_TIMEOUT 10
DWORD dwFileLen;
//unsigned char *abReadIOBuffer;
//unsigned char *abWriteIOBuffer;
unsigned char abReadIOBuffer[];
unsigned char abWriteIOBuffer[];
//char *abReadIOBuffer;
//char *abWriteIOBuffer;

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CDocumenttransmissionDlg dialog

CDocumenttransmissionDlg::CDocumenttransmissionDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CDocumenttransmissionDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CDocumenttransmissionDlg)
        // 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 CDocumenttransmissionDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CDocumenttransmissionDlg)
        // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDocumenttransmissionDlg, CDialog)
    //{{AFX_MSG_MAP(CDocumenttransmissionDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_selectfile, Onselectfile)
    ON_BN_CLICKED(IDC_receivefile, Onreceivefile)
    ON_BN_CLICKED(IDC_sendfile, Onsendfile)
    ON_BN_CLICKED(IDC_storefile, Onstorefile)
    ON_BN_CLICKED(IDC_driveropen, Ondriveropen)
    ON_BN_CLICKED(IDC_enumchannel, Onenumchannel)
    ON_BN_CLICKED(IDC_enumboard, Onenumboard)
    ON_BN_CLICKED(IDC_openchannel, Onopenchannel)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDocumenttransmissionDlg message handlers

BOOL CDocumenttransmissionDlg::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 CDocumenttransmissionDlg::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 CDocumenttransmissionDlg::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 CDocumenttransmissionDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}
/************************************************************************/
/*                    选择要传输的文件                                  */
/************************************************************************/
void CDocumenttransmissionDlg::Onselectfile() 
{
    // TODO: Add your control notification handler code here
    CFileDialog fileDlg(TRUE);
    fileDlg.m_ofn.lpstrFilter="Text Files(*.doc)\0*.doc\0picture(*.jpg)\0*.jpg\0All File(*.*)\0*.*\0\0";
    fileDlg.m_ofn.lpstrDefExt="doc";
    if (IDOK==fileDlg.DoModal())
    {
        CFile file(fileDlg.GetFileName(),CFile::modeRead|CFile::typeBinary);        
        dwFileLen=(DWORD)file.GetLength();
        abWriteIOBuffer=new unsigned char [dwFileLen+1];
        unsigned char abWriteIOBuffer[] = {0};
        abWriteIOBuffer[dwFileLen]=0;
        file.Read(abWriteIOBuffer,dwFileLen);
        file.Close();        
    }
}
/************************************************************************/
/*                  发送已经存到abWriteIOBuffer的文件                   */
/************************************************************************/
void CDocumenttransmissionDlg::Onsendfile() 
{
    // TODO: Add your control notification handler code here
    long lRet=CIFX_NO_ERROR; 
    HANDLE hDriver  = NULL;
    
    uint32_t          ulBoard    = 0;
    BOARD_INFORMATION tBoardInfo = {0};
    
    uint32_t            ulChannel    = 0;
    CHANNEL_INFORMATION tChannelInfo = {0};
    
    HANDLE hSys = NULL;
    
    HANDLE hChannel = NULL;
    
    uint32_t kbhit= 0;    
    
    lRet = xDriverOpen(&hDriver);
    if(CIFX_NO_ERROR == lRet)
    {
        while(lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo))
        {
            while(lRet=xDriverEnumChannels(hDriver, ulBoard, ulChannel, sizeof(tChannelInfo), &tChannelInfo))
            {
                ++ulChannel;
            }
            ++ulBoard;
        }
        uint32_t ulChannel=0;    
        lRet = xSysdeviceOpen(hDriver,"cifX0", &hSys);
        if(CIFX_NO_ERROR == lRet)
        {
            lRet = xChannelOpen(hDriver,"cifX0", ulChannel, &hChannel);
            if(CIFX_NO_ERROR != lRet)
            {
                MessageBox("cifX0通道0打开失败!");
            } else
            {
                // Cyclic IO data transfer
                // Write Output to all areas
                lRet = xChannelIOWrite( hChannel,0, 0,dwFileLen,abWriteIOBuffer, IO_WAIT_TIMEOUT);
                if( lRet== CIFX_NO_ERROR)
                {
                    // Error writing Output data
                    MessageBox("发送数据完成");
                }else
                {
                    MessageBox("发送数据失败");
                }
                
            }      
        }
    }
}
/************************************************************************/
/*                  接收文件并放到abReadIOBuffer                        */
/************************************************************************/
void CDocumenttransmissionDlg::Onreceivefile() 
{
    // TODO: Add your control notification handler code here
    long lRet=CIFX_NO_ERROR; 
    HANDLE hDriver  = NULL;
    
    uint32_t          ulBoard    = 0;
    BOARD_INFORMATION tBoardInfo = {0};
    
    uint32_t            ulChannel    = 0;
    CHANNEL_INFORMATION tChannelInfo = {0};
    
    HANDLE hSys = NULL;
    
    HANDLE hChannel = NULL;    
    
    lRet = xDriverOpen(&hDriver);
    if(CIFX_NO_ERROR == lRet)
    {
        while(lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo))
        {
            while(lRet=xDriverEnumChannels(hDriver, ulBoard, ulChannel, sizeof(tChannelInfo), &tChannelInfo))
            {
                ++ulChannel;
            }
            ++ulBoard;
        }
        uint32_t ulChannel=0;
        lRet = xSysdeviceOpen(hDriver,"cifX0", &hSys);
        if(CIFX_NO_ERROR == lRet)
        {
            lRet = xChannelOpen(hDriver,"cifX0", ulChannel, &hChannel);
            if(CIFX_NO_ERROR != lRet)
            {
                MessageBox("打开失败!");
            } else
            {                                 
                lRet = xChannelIORead( hChannel,0, 0,100 ,abReadIOBuffer, IO_WAIT_TIMEOUT);
                if(lRet==CIFX_NO_ERROR)    //经过这一步可以把数据从装置读取到"abReadIOBuffer",以供显示程序使用
                {
                    MessageBox("存入abReadbuffer!");
                }                                 
            }
        }              
    }    
}
/************************************************************************/
/*                 把接收到的文件另存到指定的位置                       */
/************************************************************************/
void CDocumenttransmissionDlg::Onstorefile() 
{
    // TODO: Add your control notification handler code here
    CFileDialog fileDlg(FALSE);
    fileDlg.m_ofn.lpstrFilter="Text Files(*.doc)\0*.doc\0*.jpg\0All File(*.*)\0*.*\0\0";
    fileDlg.m_ofn.lpstrDefExt="doc";
    
    if (IDOK==fileDlg.DoModal())
    {
        CFile file(fileDlg.GetFileName(),CFile::modeCreate|CFile::modeWrite);
        file.Write(abReadIOBuffer,dwFileLen);
        file.Close();        
    }
}
/************************************************************************/
/*                      测试用的一些按钮                                */
/************************************************************************/


void CDocumenttransmissionDlg::Ondriveropen() 
{
    // TODO: Add your control notification handler code here
    long lRet; 
    HANDLE hDriver  = NULL;
    
    lRet = xDriverOpen(&hDriver);
    if(CIFX_NO_ERROR != lRet)
    {
        MessageBox("打开失败!");    
    }
    else
    {
        MessageBox("打开成功!");
    }
}

void CDocumenttransmissionDlg::Onenumchannel() 
{
    // TODO: Add your control notification handler code here
    /************************************************************************/
    /*                                先打开驱动                            */
    /************************************************************************/
    /************************************************************************/
    /*                             列举board                                */
    /************************************************************************/
    long lRet;
    HANDLE hDriver  = NULL;
    
    uint32_t          ulBoard    = 0;
    BOARD_INFORMATION tBoardInfo = {0};
    
    uint32_t            ulChannel    = 0;
    CHANNEL_INFORMATION tChannelInfo = {0};    
    
    
    lRet = xDriverOpen(&hDriver);
    if(CIFX_NO_ERROR == lRet)
    {
        lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo);
        /*************************************************************************/
        /* iterate over all channels on the current board */
        lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo);
        if(CIFX_NO_ERROR != lRet)
        {
            MessageBox("枚举board0失败!");    
        }
        else
        {
            lRet=xDriverEnumChannels(hDriver, ulBoard, ulChannel, sizeof(tChannelInfo), &tChannelInfo);
            if(CIFX_NO_ERROR != lRet)
            {
                MessageBox("枚举board0通道0失败!!");    
            }
            else
            {
                MessageBox("枚举board0通道0成功!");
                
                ++ulChannel;
                lRet=xDriverEnumChannels(hDriver, ulBoard, ulChannel, sizeof(tChannelInfo), &tChannelInfo);
                if(CIFX_NO_ERROR != lRet)
                {
                    MessageBox("枚举board0通道1失败!");    
                }
                else
                {
                    MessageBox("枚举board0通道1成功!");                    
                }
            }
            
            ++ulBoard;
            uint32_t  ulChannel=0;
            lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo);
            if(CIFX_NO_ERROR != lRet)
            {
                MessageBox("枚举board1失败!");    
            }
            else
            {
                lRet=xDriverEnumChannels(hDriver, ulBoard, ulChannel, sizeof(tChannelInfo), &tChannelInfo);
                if(CIFX_NO_ERROR != lRet)
                {
                    MessageBox("枚举board1通道0失败!");    
                }
                else
                {
                    MessageBox("枚举board1通道0成功!");
                    
                    ++ulChannel;
                    lRet=xDriverEnumChannels(hDriver, ulBoard, ulChannel, sizeof(tChannelInfo), &tChannelInfo);
                    if(CIFX_NO_ERROR != lRet)
                    {
                        MessageBox("枚举board1通道1失败!");    
                    }
                    else
                    {
                        MessageBox("枚举board1通道1成功!");                    
                    }
                    
                    ++ulBoard;
                    lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo);
                    if(CIFX_NO_ERROR != lRet)
                    {
                        MessageBox("枚举board2失败!");    
                    }
                    else
                    {
                        MessageBox("枚举board2成功!");
                        ++ulBoard;
                    }
                }    
            }
        }
    }
}

void CDocumenttransmissionDlg::Onenumboard() 
{
    // TODO: Add your control notification handler code here
    long lRet;
    HANDLE hDriver  = NULL;
    
    uint32_t          ulBoard    = 0;
    BOARD_INFORMATION tBoardInfo = {0};    
    
    /* Iterate over all boards */
    lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo);
    if(CIFX_NO_ERROR != lRet)
    {
        MessageBox("打开失败!");    
    }
    else
    {
        MessageBox("打开成功0!");
        ++ulBoard;
        lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo);
        if(CIFX_NO_ERROR != lRet)
        {
            MessageBox("打开失败!");    
        }
        else
        {
            MessageBox("打开成功1!");
            ++ulBoard;
            lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo);
            if(CIFX_NO_ERROR != lRet)
            {
                MessageBox("打开失败!");    
            }
            else
            {
                MessageBox("打开成功1");
                ++ulBoard;
            }
        }    
    }    
}

void CDocumenttransmissionDlg::Onopenchannel() 
{
    // TODO: Add your control notification handler code here
    /************************************************************************/
    /*                              先打开驱动                              */
    /************************************************************************/
    /************************************************************************/
    /*                              列举 board                              */
    /************************************************************************/
    /************************************************************************/
    /*                              列举 channel                            */
    /************************************************************************/
    /************************************************************************/
    /*                              SysdeviceOpen                           */
    /************************************************************************/
    long lRet; 
    HANDLE hDriver  = NULL;
    
    uint32_t          ulBoard    = 0;
    BOARD_INFORMATION tBoardInfo = {0};
    
    uint32_t            ulChannel    = 0;
    CHANNEL_INFORMATION tChannelInfo = {0};
    
    //char* szBoard;
    HANDLE hSys = NULL;
    
    HANDLE hChannel = NULL;
    
    lRet = xDriverOpen(&hDriver);
    if(CIFX_NO_ERROR == lRet)
    {
        while(lRet=xDriverEnumBoards(hDriver, ulBoard, sizeof(tBoardInfo), &tBoardInfo))
        {
            while(lRet=xDriverEnumChannels(hDriver, ulBoard, ulChannel, sizeof(tChannelInfo), &tChannelInfo))
            {
                ++ulChannel;
            }
            ++ulBoard;
        }
        uint32_t            ulChannel= 0;
        lRet = xSysdeviceOpen(hDriver,"cifX0", &hSys);
        if(CIFX_NO_ERROR == lRet)
        {
            lRet = xChannelOpen(hDriver,"cifX0", ulChannel, &hChannel);
            if(CIFX_NO_ERROR != lRet)
            {
                MessageBox("cifX0通道0打开失败!");    
            }
            else
            {
                MessageBox("cifX0通道0打开成功!");
            }
        }    
    }
}
2011-12-13 13:09
shixi2011
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2011-5-12
收藏
得分:0 
回复 2楼 hellovfp
后面有一部分是测试用的按钮,不用管
2011-12-13 13:10
shixi2011
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2011-5-12
收藏
得分:0 
回复 2楼 hellovfp
其中的“发送已经存到abWriteIOBuffer的文件”和“接收文件并放到abReadIOBuffer”就是我调用API函数的过程;
“选择要传输的文件”是选择文件存入buffer的过程,“把接收到的文件另存到指定的位置”就是“另存为”文件的过程
2011-12-13 13:13
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 
直接用//unsigned char *abReadIOBuffer = 0;
//unsigned char *abWriteIOBuffer = 0;
这两个定义啊,不要用unsigned char abReadIOBuffer[]; 要么定义unsigned char abReadIOBuffer[1024 * 50],程序就不能用new操作符,new返回的始终是对象的地址。

如果用这个定义,调用那个API出错的话,就要查该API返回值所代表的意义了。

我们都在路上。。。。。
2011-12-13 14:42
shixi2011
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2011-5-12
收藏
得分:0 
回复 6楼 hellovfp
谢谢了,已经解决了
unsigned char abWriteIOBuffer[100] = {0};
abWriteIOBuffer是栈上的,不用new了
去了这个直接用abWriteIOBuffer就可以但是还有点小问题,嘿嘿
https://bbs.bccn.net/viewthread.php?tid=357700&extra=page%3D1&frombbs=1
2011-12-14 10:09
快速回复:指针和数组的问题(文件操作CFile)
数据加载中...
 
   



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

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