控制台程序,怎样以最快的数度读出文本文件中的第n行数据? 查找 '\n' or '\r',并计数,然后读取你需要的数据。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把整个文件读到内存里面,然后strchr('\');n次 把整个文件读到buf里面,然后strchr(buf,'\r');n次 把整个文件读到buf里面,然后strchr(buf,'\r');n-1次fgets(anotherbuf); \\我得到emailBuf后,该怎么做,请指点,谢谢! char * emailBuf; FILE *stream; struct _stat buf; int result,filesize,numread; //获得文件大小---begin result = _stat("d:\\emaillist.txt", &buf ); if( result == 0 ) { filesize=buf.st_size; }//获得文件大小---end//读取文件----begin if( (stream = fopen("d:\\emaillist.txt", "r+t" )) != NULL ) { emailBuf=new char[filesize+1]; numread=fread(emailBuf,1, filesize, stream ); emailBuf[numread]='\0'; fclose( stream ); }//get file end//下面的算法怎么写? 偶试一下:假使偶要读第n行的内容!char szText[256];memset (szText, 0, sizeof(szText));int nStartIndex = 0;int nIndex = 0;int nLine = 0;while(nIndex <= numread){if (emailBuf[nIndex++] == '\n'){nLine ++;if (nLine == n-1) nStartIndex = nIndex;if (nLine == n) {strncpy (szText, emailBuf + nStartIndex, nIndex - nStartIndex-1-1);break;}}} 谢谢大家;to whitewaterbluesky:strncpy (szText, emailBuf + nStartIndex, nIndex - nStartIndex-1-1);应该为strncpy (szText, emailBuf + nStartIndex, nIndex - nStartIndex-1); 呃..很久没来逛了,问一个关于VS2005状态栏的问题 大家%&#*#%,google会退出中国吗? 简单的问题 MFC下循环创建多线程 请问 CAxWindow CreateControl 创建的控件如何销毁? 怎样将Win32控制台程序转化为可视化程序 如何在dxShow播放的同时,截获键盘消息? 在Windows系统中定时的问题。。。急!!!! 关于票据的格式打印 跨进程读取“系统计算器”中的内存数据时报错 大家来讨论一下这个程序的实现技术细节 WIN2000 FTP服务问题?大送分?救命? 请高手赐教,关于WINSOCK网络初学者的编程的问题,一定很简单,抢分问题
strchr('\');n次
把整个文件读到buf里面,然后
strchr(buf,'\r');n次
strchr(buf,'\r');n-1次
fgets(anotherbuf);
\\我得到emailBuf后,该怎么做,请指点,谢谢!
char * emailBuf;
FILE *stream;
struct _stat buf;
int result,filesize,numread;
//获得文件大小---begin
result = _stat("d:\\emaillist.txt", &buf );
if( result == 0 )
{
filesize=buf.st_size;
}
//获得文件大小---end
//读取文件----begin
if( (stream = fopen("d:\\emaillist.txt", "r+t" )) != NULL )
{
emailBuf=new char[filesize+1];
numread=fread(emailBuf,1, filesize, stream );
emailBuf[numread]='\0';
fclose( stream );
}//get file end//下面的算法怎么写?
假使偶要读第n行的内容!char szText[256];memset (szText, 0, sizeof(szText));int nStartIndex = 0;
int nIndex = 0;int nLine = 0;while(nIndex <= numread)
{
if (emailBuf[nIndex++] == '\n')
{
nLine ++;
if (nLine == n-1) nStartIndex = nIndex;
if (nLine == n)
{
strncpy (szText, emailBuf + nStartIndex, nIndex - nStartIndex-1-1);
break;
}
}
}
to whitewaterbluesky:
strncpy (szText, emailBuf + nStartIndex, nIndex - nStartIndex-1-1);
应该为
strncpy (szText, emailBuf + nStartIndex, nIndex - nStartIndex-1);