请问怎么得到abc.txt内的数量?
FILE *fp;
fp=fopen("abc.txt", "r");
.......
abc里面是四个1,怎么才得到里面的数量呢?如果太多的话是不可能静态指定的啊
问题点数:30、回复次数:6Top
1 楼ostrich2fly(ostrich2fly)回复于 2006-03-04 08:18:34 得分 0
没看明白什么意思
Top
2 楼jobs002(Oh! Office)回复于 2006-03-04 08:24:41 得分 0
ifstream fin("test.txt");
int GetFromFile[100];
int m;
fin>>GetFromFile;
m=sizeof(GetFromFile)/sizeof(m);
Top
3 楼vcmute(BCare4 H1Rest Good9!)回复于 2006-03-04 09:46:00 得分 0
fseek(fp,0,SEEK_END);
int filelen=ftell(fp);Top
4 楼84shelley(影朔)回复于 2006-03-04 10:11:30 得分 0
这个类定义了一组文本操作,具体用到什么你自己看
//Text.h
#ifndef _INC_TEXT
#define _INC_TEXT
/////////////////////////////
//类名:CText
//作者:shelley
//最后修改时间:2005/02/03
//说明:提供对文本的基础操作,并作为基类为其他基于文本的类提供基本组成功能
////////////////////////////
class CText
{
public:
CText();
virtual ~CText();
public:
CString GetText(void);
BOOL OpenText(void);
UINT GetRowCount(void);
char* GetCurrentPointer(void);
UINT GetCharCount(char flag);//获取指定字符个数
char* GetCharPointer(char flag, int no);//返回指定字符的指针,如果返回为NULL则表示操作失败,第二个参数表示要查找的字符存在的个数,其最小值为1。
protected:
CString m_strFileName;
CString m_strFilePath;
CFile *m_pFile;
BOOL m_bOpen;
char *m_pBuf;
char *lpBuf;
char *m_pEndBuf;
UINT m_RowCount;
protected:
BOOL CloseText(void);
void MoveToBegin(void);
inline UINT RowCount(void);//仅提供类及子类的OpemText()使用
CString GetChars(UINT begin, UINT end);
BOOL IsOpen(void);
BOOL NextRow(void);
};
#endif //_INC_TEXT
//Text.cpp
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "..\AudioDataBase.h"
#include "Text.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CText::CText()
{
//m_strFileName = FileName;
//m_strFilePath = FilePath;
m_bOpen = FALSE;
m_pFile = new CFile();
m_pBuf = (char*)malloc(sizeof(char)*65535);
m_pEndBuf = &m_pBuf[65534];
}
CText::~CText()
{
//m_pFile->Close();
delete m_pFile;
free(m_pBuf);
}
BOOL CText::OpenText(void)
{
if (IsOpen())
{
this->CloseText();
}
CString temp = this->m_strFilePath+"\\"+this->m_strFileName;
if (m_pFile->Open(temp, CFile::modeRead))
{
m_bOpen = TRUE;
m_pFile->Read(m_pBuf, 65535);
lpBuf = m_pBuf;
m_RowCount = RowCount();
return TRUE;
}
return FALSE;
}
BOOL CText::CloseText(void)
{
if (!IsOpen())
{
return FALSE;
}
m_pFile->Close();
return TRUE;
}
CString CText::GetText(void)
{
if (!IsOpen())
{
this->OpenText();
}
return lpBuf;
}
BOOL CText::IsOpen()
{
return m_bOpen;
}
BOOL CText::NextRow()
{
if (!IsOpen())
{
this->OpenText();
}
while (*lpBuf!=13)
{
lpBuf++;
if (lpBuf==m_pEndBuf)
{
return FALSE;
}
}
lpBuf += 2;
return TRUE;
}
char* CText::GetCharPointer(char flag, int no)
{
if (no>GetCharCount(flag))
return NULL;
if (no<1)
no = 1;
char *pTemp;
pTemp = lpBuf;
for (UINT i=0; i<no; i++)
{
while ((*pTemp!=flag)&&(*pTemp!=0))
{
pTemp++;
}
pTemp++;
}
pTemp--;
return pTemp;
}
UINT CText::GetCharCount(char flag)
{
if (!IsOpen())
{
this->OpenText();
}
UINT Count = 0;
char *pTemp;
pTemp = m_pBuf;
while (*pTemp!=0)
{
if (*pTemp==flag)
{
Count++;
}
pTemp++;
}
return Count;
}
void CText::MoveToBegin(void)
{
if (!this->IsOpen())
{
this->OpenText();
}
lpBuf = m_pBuf;
}
char* CText::GetCurrentPointer(void)
{
if (!this->IsOpen())
{
this->OpenText();
}
return lpBuf;
}
UINT CText::GetRowCount(void)
{
if (!this->IsOpen())
{
this->OpenText();
}
return m_RowCount;
}
UINT CText::RowCount(void)
{//内部功能模块函数
UINT Count = 0;
char *temp;
temp = lpBuf;
while (*temp!=0)
{
if (*temp==13)
{
Count++;
}
temp++;
}
return Count;
}
CString CText::GetChars(UINT begin, UINT end)
{
if (end<begin)
{
return "";
}
if (!IsOpen())
{
this->OpenText();
}
char *temp;
char *Buf;
temp = (char*)malloc(sizeof(char)*65535);
Buf = (char*)malloc(sizeof(char)*65535);
CString str = this->GetText();
strcpy(Buf, str);
CString Chars = "";
for (int i=0; i<(end-begin); i++)
{
temp[i] = Buf[begin+i];
}
temp[i] = 0;
Chars = temp;
free(Buf);
free(temp);
return Chars;
}Top
5 楼zengwujun(月之海 为linux入门奋斗100天)回复于 2006-03-04 10:52:44 得分 0
FILE *fp;
fp=fopen("abc.txt", "r");
char sz[8];
fread(sz, sizeof(int), 1, fp);
int cnt = atoi(sz);
如果有多个整数,就循环处理,并用fseek定位Top
6 楼skfox(sky)回复于 2006-03-04 15:16:40 得分 0
没明白lz的意思Top




