怎么读取文本文件中指定行?

icecools 2003-10-30 12:49:58
不用标准库,怎么能比较快的读出来啊?
...全文
390 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
icecools 2003-10-30
  • 打赏
  • 举报
回复
谢谢各位!
我在unix下开发,所以BCB用不上了,其实我一直在想有没有可以直接定位到行的方法,看来是没有了,一定要自己loop才可以
给分了!
dximg 2003-10-30
  • 打赏
  • 举报
回复
/* 上面的写错了 */
/* 为了方便,将第n行直接读到标准输出 */
#include <fcntl.h>

#define BUFSIZE 1024
int bin[BUFSIZE];

int
rline(char *fname, int n)
{
int i, fd, *p;

if ((fd = open(fname, O_RDONLY)) < 0)
return 0;
while (--n > 0) {
i = 0;
do {
if (i-- <= 0) {
i = read(fd, bin, BUFSIZE);
p = bin;
if (i-- <= 0)
return 0;
}
} while (*p++ != '\n');
}
goto nextline;

i = 0;
do {
nextline:
if (i-- <= 0) {
i = read(fd, bin, BUFSIZE);
p = bin;
if (i-- <= 0)
return 0;
}
write(1, p, 1);
} while (*p++ != '\n');

retrun 0;
}

dximg 2003-10-30
  • 打赏
  • 举报
回复
/* 为了方便,将第n行直接读到标准输出 */
#include <fcntl.h>

#define BUFSIZE 1024
int bin[BUFSIZE];

int
rline(char *fname, int n)
{
int i, fd, *p;

if ((fd = open(fname, O_RDONLY)) < 0)
return 0;
while (--n > 0) {
i = 0;
do {
if (i-- <= 0) {
i = read(fd, bin, BUFSIZE);
p = bin;
if (i-- <= 0)
return 0;
}
} while (*p++ != '\n');
}
write(1, p, i);

i = 0;
do {
if (i-- <= 0) {
i = read(fd, bin, BUFSIZE);
p = bin;
if (i-- <= 0)
return 0;
}
write(1, p, 1);
} while (*p++ != '\n');

retrun 0;
}
021850524 2003-10-30
  • 打赏
  • 举报
回复
如果用'\n'作为一行结束的标志,判断'\n'数就能读了.
我不懂电脑 2003-10-30
  • 打赏
  • 举报
回复
c++ builder
TMemo *Memo = new TMemo(NULL);

Memo->Lines->LoadFromFile("c:\\a.txt");

AnsiString str = Memo->Lines->Strings[5];
cyj2008 2003-10-30
  • 打赏
  • 举报
回复

你自己对下面代码稍做修改即可

// TextReader.h: interface for the TextReader class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_TEXTREADER_H__FD4216E6_57B8_479E_8060_424FB019A5AD__INCLUDED_)
#define AFX_TEXTREADER_H__FD4216E6_57B8_479E_8060_424FB019A5AD__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include<stdio.h>

#define BUFF_SIZE 1024
#define TMP_SIZE 256
#define LAST_BLOCK 1

class TextReader
{
private:
FILE *_pFile; //文件指针
char _buff[BUFF_SIZE]; //文件缓冲区
char _tmpbuff[TMP_SIZE];//临时缓冲区
int _ntmpSize; //临时缓冲区所用的字节数
int _nReadSize; //读入缓冲区的字节数
int _ncurPos; //当前访问位置
int _blastBlock; //最后块标志
int _nrowCount; //文件行计数

void init(FILE *pF); //初始化函数,仅共内部调用
int load(); //装载缓冲区
public:
TextReader();
TextReader(FILE *pF);
virtual ~TextReader();
void Attach(FILE *pF);
void Close();
void Rewind();
int GetChar(char &ch);
int CurrentRowCount();
int ReadLine(char *buff,int &nsize);
};

#endif // !defined(AFX_TEXTREADER_H__FD4216E6_57B8_479E_8060_424FB019A5AD__INCLUDED_)

// TextReader.cpp: implementation of the TextReader class.
//
//////////////////////////////////////////////////////////////////////
#include "TextReader.h"
#include<memory.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

TextReader::TextReader()
{
init(NULL);
}

TextReader::TextReader(FILE *pFile)
{
init(pFile);
}
void TextReader::init(FILE *pFile)
{
_blastBlock=!LAST_BLOCK;
_ncurPos=0;
_nReadSize=0;
_ntmpSize=0;
_pFile=pFile;
_nrowCount=0;
memset((char *)_tmpbuff,0,sizeof(_tmpbuff));
memset((char *)_buff,0,sizeof(_buff));
}
TextReader::~TextReader()
{
if(_pFile)
fclose(_pFile);
}
void TextReader::Attach(FILE *pFile)
{
if(_pFile!=NULL)
fclose(_pFile);
init(pFile);
}
void TextReader::Close()
{
fclose(_pFile);
init(NULL);
}
void TextReader::Rewind()
{
if(_pFile!=NULL)
rewind(_pFile);
init(_pFile);
}

int TextReader::load()
{
if(_pFile==NULL)
return 0;
if(_blastBlock)
{
_nReadSize=0;
_ncurPos=0;
return 0;
}
_nReadSize=fread((char *)_buff,sizeof(char),BUFF_SIZE,_pFile);
if(_nReadSize<BUFF_SIZE)
_blastBlock=LAST_BLOCK;
else
_blastBlock=!LAST_BLOCK;
_ncurPos=0;
return 1;
}

int TextReader::CurrentRowCount()
{
return _nrowCount;
}

int TextReader::GetChar(char &ch)
{
if(_ncurPos>=_nReadSize&&!load())
return 0;
if(_nReadSize>0)
{
ch=_buff[_ncurPos];
_ncurPos++;
if(ch==0x0a) _nrowCount++;
return 1;
}
return 0;
}

int TextReader::ReadLine(char *buff,int &nsize)
{
if(_ncurPos>=_nReadSize&&!load())
return 0;
int length=0,i;
while(1)
{
for(i=_ncurPos;i<_nReadSize&&_buff[i]!=0x0a;i++);
length=i-_ncurPos;//get string line length
if(length==0)
{
if(_ntmpSize>0){
memcpy((char *)buff,(char *)_tmpbuff,_ntmpSize);
nsize=_ntmpSize;
_ntmpSize=0;
_nrowCount++;
buff[nsize]='\0';
return 1;
}
if(i>=_nReadSize){
if(!load()) return 0;
}
else{
_nrowCount++;
_ncurPos=i+1;
if(_ncurPos>=_nReadSize) { if(!load()) return 0;}
}
}
else
{
if(i>=_nReadSize){
_ntmpSize=length;
memcpy((char *)_tmpbuff,(char *)&_buff[_ncurPos],length);
load();
}
else
{ int ncount=0;
if(_ntmpSize>0)
{
memcpy((char *)buff,(char *)_tmpbuff,_ntmpSize);
ncount=_ntmpSize;
_ntmpSize=0;
}
memcpy((char *)buff+ncount,(char *)&_buff[_ncurPos],length);
_ncurPos=i+1;
_nrowCount++;
buff[length+ncount]='\0';
nsize=length+ncount;
return 1;
}
}
}
return 0;
}


69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧