我现在想把一个txt类型的文件内容导入到oracle数据库中,例如文件如下:
 刘德华,男,22,江苏,90,80,70
 张学友,男,33,山西,87,93,67
 孙燕姿,女,23,吉林,67,85,72该怎样读这个文件,保证每一个记录能都写入到数据库中.是不是要一个结构体做缓冲??
先读一个记录到机构体中,再写入数据库,再读下一个记录.如果是的话,那么下一个记录该怎么读呢?

解决方案 »

  1.   

    用fgets一行一行读到结构体中
      

  2.   

    I am not familiar with oracle.
    But I think the method to fulfil your purpose is 
     very similiar to which we to with Database like access.
    You can use something like JDBC or ADO or DAO,etc .to open 
    the source database in oracle format,then open your
     text file ,read the records one by one from text file,
    then use ado or dao or JDBC to write the content into 
    the oracle database .
        
      

  3.   

    一行一行的读,或者每个记录做一个结构,如果记录中的元素个数是固定的话,比如向你这样的例子,一个记录有7个属性,第8*i个读入的属性就是第i+1个记录中的。
    这里可能要用到一些文件操作的函数,你可以在MSDN里查一下。
      

  4.   

    to lovetheme:
      This is the problem of VC++,which has no business with oracle.I want to solve this problem only with VC++,not through oracle.I think we can solve it whatever the database is in the environment of VC++.
      

  5.   

    我帮你查了一下,CFile类中有这些函数:
    virtual UINT Read( void* lpBuf, UINT nCount );
    第一个参数是接受读入字符的缓冲区,第二个参数是读入字符的最大字节数。
      

  6.   

    我有一个读文件的类,用GetWord()就可以返回一个个的单词或一个汉字
    // ParseText.cpp: implementation of the CParseText class.
    //
    //////////////////////////////////////////////////////////////////////#include "stdafx.h"
    #include "ParseFile.h"
    #include "ParseText.h"#ifdef _DEBUG
    #undef THIS_FILE
    static char THIS_FILE[]=__FILE__;
    #define new DEBUG_NEW
    #endif//////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    const int iSeparatorCount=15;
    CParseText::CParseText()
    {
    m_dwFileLen = 0;
    m_pData = NULL;
    m_nPosition=0;
    for(int i=0;i<0xff;i++)
    m_aSeparator[i]=FALSE;
    char cSperator[iSeparatorCount]=
    {' ','\t','\n',',','.'
    ,';','(',')','{','}'
    ,'[',']','!','<','>'
    };
    for(i=0;i<iSeparatorCount;i++)
    m_aSeparator[cSperator[i]]=TRUE; m_strFileName="d:\\tests.txt";//开的文件名
    CFile file;
    if(file.Open(m_strFileName,CFile::modeRead,NULL))
    {
    m_dwFileLen=file.GetLength();
    if(m_dwFileLen>0)
    {
    m_pData = new BYTE[m_dwFileLen];
    file.SeekToBegin();
    file.ReadHuge((LPVOID)m_pData,m_dwFileLen);
    file.Close();
    }
    }
    if(m_pData)
    {
    while(m_nPosition<m_dwFileLen)
    {
    CString ret;
    GetWord(ret); 
    }
    }
    }CParseText::~CParseText()
    {}
    //功能:得到英语单词,或汉字的字
    //参数:返回的字串
    BOOL CParseText::GetWord(CString& strReturn)
    {
    strReturn.Empty();
    ASSERT(m_pData);
    StateType state=START;
    if(m_dwFileLen<2)
    return FALSE;
    BYTE CurrentChar,FollowChar;
    int iChiCount=0;//中文的字数
    while(state!=DONE)
    {
    BOOL save=FALSE;
    CurrentChar = m_pData[m_nPosition++];
    FollowChar= m_pData[m_nPosition];
    if(m_aSeparator[CurrentChar]) //分隔符
    state=DONE;
    //英语字母及数字,可能有"3rd"
    else if((CurrentChar>='0'&&CurrentChar<='9')||
    (CurrentChar>='a'&&CurrentChar<='z')||
    (CurrentChar>='A'&&CurrentChar<='Z'))
    {
    state=ENGCHAR;
    }
    //汉字的处理
    else if(CurrentChar>0x7f) 
    state=CHICHAR;
    switch(state)
    {
    case ENGCHAR:
    save=TRUE;
    break;
    case CHICHAR:
    save=TRUE;
    iChiCount++;
    break;
    case DONE:  
    save=FALSE;
    state=DONE;
    break;
    }
    if(save)
    {
    strReturn+=CurrentChar;
    if(state==CHICHAR&&iChiCount==2)state=DONE;
    }
    }
    return TRUE;
    }
    typedef enum StateType
    {
    START,CHICHAR,ENGCHAR,DONE
    };
    class CParseAdpter
    {
    public:
    //当找到一个单词进,就
    virtual int OnKeyFind(LPCTSTR lpszKey);
    };
    ///.h
    class CParseText  
    {
    public:
    //文件名
    CParseText();
    CParseText(CParseAdpter * pAdpter,LPCTSTR lpszFilename);
    //字符串指针
    CParseText(CParseAdpter * pAdpter,BYTE * pData);
    virtual ~CParseText();
    int * GetKeyIDArray();
    private:
    void ArraySort();
    void SetupStringPosArray();
    BOOL StrComp(PBYTE pbStr1,PBYTE pbStr2,int iStrLen2);
    BOOL GetWord(CString& strReturn);//得到一个单词或汉字
    BOOL m_aSeparator[255];//分隔符数组
    UINT m_nPosition;
    CString m_strFileName;
    DWORD m_dwFileLen;
    PBYTE m_pData;};
      

  7.   

    MFC类中有一行一行读写的函数TXT中的回车指是一个字符。也是可以读出来的
      

  8.   

    to adams_here:
     我刚才查了一下MSDN,read只能读第一行,那么第二个记录怎么办?
      

  9.   

    to csdn_lee: 
    thank you very much!y our programm give me some great elicitation.
      

  10.   

    CStdFile file1;
    file1.readstring(..................)
    可以读如一行!!
    然后提交数据库!!!
      

  11.   

    我记得很多数据库软件在导出数据库的时候有一种 CSV 格式的输出,其实就跟你的文本格式差不多,你可以试试直接用数据库接口打开该文本文件并读记录,或者先用Excel或Access之类先把它转换成标准的你能处理的数据库格式文件再用你的软件读取。
      

  12.   

    to zkg751206(zjg):
    我看了一下文档,你说的类函数确实有这个功能,但是文档里没有说在什么时候读下一行,只是说他有re功能.还望告知:什么时候读下一行?
      

  13.   

    BOOL ReadFile(const char *filename)
    {
    char szLine[1024];
    FILE *fp = fopen(filename, "rb");
      if(fp == NULL){
    return FALSE;
    }
    while(!feof(fp))
    {
    if(fgets(szLine, sizeof(szLine), fp) == NULL)
    break;
    sscanf(szLine, "%s,%s,%s,%s", szName, sz...
    ...
    }
     
    我真不愿意写,这么基础的东西:)