我现在想把一个txt类型的文件内容导入到oracle数据库中,例如文件如下:
刘德华,男,22,江苏,90,80,70
张学友,男,33,山西,87,93,67
孙燕姿,女,23,吉林,67,85,72该怎样读这个文件,保证每一个记录能都写入到数据库中.是不是要一个结构体做缓冲??
先读一个记录到机构体中,再写入数据库,再读下一个记录.如果是的话,那么下一个记录该怎么读呢?
刘德华,男,22,江苏,90,80,70
张学友,男,33,山西,87,93,67
孙燕姿,女,23,吉林,67,85,72该怎样读这个文件,保证每一个记录能都写入到数据库中.是不是要一个结构体做缓冲??
先读一个记录到机构体中,再写入数据库,再读下一个记录.如果是的话,那么下一个记录该怎么读呢?
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 .
这里可能要用到一些文件操作的函数,你可以在MSDN里查一下。
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++.
virtual UINT Read( void* lpBuf, UINT nCount );
第一个参数是接受读入字符的缓冲区,第二个参数是读入字符的最大字节数。
// 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;};
我刚才查了一下MSDN,read只能读第一行,那么第二个记录怎么办?
thank you very much!y our programm give me some great elicitation.
file1.readstring(..................)
可以读如一行!!
然后提交数据库!!!
我看了一下文档,你说的类函数确实有这个功能,但是文档里没有说在什么时候读下一行,只是说他有re功能.还望告知:什么时候读下一行?
{
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...
...
}
我真不愿意写,这么基础的东西:)