论坛
门户
内部优惠
喜欢
话题
VIP会员
搜索
新浪微博
登录
注册
100%
100%
首页
>
软件开发
>
C/C++语言
>
用c语言写的读一行源代码
回复
« 返回列表
灯火互联
管理员
注册日期
2011-07-27
发帖数
41778
QQ
火币
41290枚
粉丝
1086
关注
100
加关注
写私信
打招呼
阅读:
3437
回复:
0
用c语言写的读一行源代码
楼主
#
更多
只看楼主
倒序阅读
发布于:2013-02-19 14:19
保存
100%
100%
[]
1
因为工作需要,用
c语言
写了一个读一行的函数,供
大家
参考:
[cpp]
static BOOL readLine(FILE* file, char** ppBuf)
{
int nMax = 128;
char szBuf[128];
char* pAmountBuf = NULL ;
char* pResult;
int nLen = 0;
int nAmountLen = 0;
char* pTmp;
BOOL bLoop = TRUE;
do
{
pResult=fgets(szBuf, nMax, file);
/* handle error */
if( pResult == NULL ;; !feof(file) )
{
if( pAmountBuf )
free( pAmountBuf);
return FALSE;
}
if( pResult == NULL )
{
break;
}
else if ( (nLen = strlen( szBuf )) < nMax-1 ||
szBuf[nLen-1] == '\n'
)
{
if( nLen ==0 )
break;
bLoop = FALSE;
}
nAmountLen += nLen;
pTmp = (char*)malloc( sizeof(char)* (nAmountLen + 1) );
if( pTmp == NULL )
{
if( pAmountBuf )
free ( pAmountBuf );
return FALSE;
}
if( pAmountBuf )
{
strcpy(pTmp, pAmountBuf );
strcat(pTmp, szBuf);
}
else
{
strcpy(pTmp, szBuf);
}
pAmountBuf = pTmp;
}
while( bLoop );
if( nAmountLen!=0 ;; pAmountBuf[nAmountLen-1] == '\n' )
{
pAmountBuf[--nAmountLen]=0;
}
if( nAmountLen!=0 ;; pAmountBuf[nAmountLen-1] == '\r' )
{
www.atcpu.com
pAmountBuf[--nAmountLen]=0;
}
if( nAmountLen==0 )
{
if( pAmountBuf )
free( pAmountBuf );
}
else
*ppBuf=pAmountBuf;
return nAmountLen!=0;
}
喜欢
0
评分
0
最新喜欢:
淘宝天猫隐藏优惠券地址
回复
100%
发帖
回复
« 返回列表
普通帖
您需要登录后才可以回帖,
登录
或者
注册
100%
返回顶部
关闭
最新喜欢