
FILE *file = NULL
char szFile[1025] = {0}
int nHadRead = 0
file = fopen( "file.txt", "r+")
if ( file == NULL )
return
fseek( file, 0, SEEK_END )//定位到文件尾
int nLen = ftell( file )//获取当前位置,即文件长度
fseek( file 0, SEEK_SET )//重新定位到文件开头,准备开始读
while ( nHadRead <nLen )
{
int nRead = nLen - nHadRead >1024 ? 1024 : nLen - nHadRead //如果剩余小于1024字节,则读剩余字节,否则每次读取1024字节。
int nTmp = fread( szFile, 1, nRead , file )
nHadRead += nTmp
printf( "%s", szFile )
memset( szFile, 0x0, sizeof(szFile) )
}
fclose(file)
大致过程就是这样,纯手打,没有调试过,可能有错
用fgets一行行读入,读到EOF结束。
每读一行用
下面函数找到address
后面方括号
i1,i2
位置
void
f1(char
*str,
int
*i1,
int
*i2){
int
i,j=0,L
L
=
strlen(str)
if
(L
<
8)
{
printf("no
address
line\n")
*i1=0
*i2=0}
for
(i=0i<Li++){
if
(
strncmp(str+i,"address",7)==0)
{j=i+7
break}
}
for
(i=ji<Li++){
if
(str[i]=='[')
*i1=i
if
(str[i]==']')
{
*i2=i
break}
}
}
从这行字符串中取出地址
str[i1+1]
到
str[i2-1],
写入文件。
调用例子:
main()
{
char
str[400]=".......address..[123
bay
street
Apt.
5]
abcdb
ssks
567
"
char
add[80]
int
i,j,i1,i2
f1(&str[0],&i1,&i2)
printf("%d
%d",i1,i2)
for
(i=i1+1i<i2i++)
{
j=i-i1-1
add[j]=str[i]}
add[j+1]='\0'
printf("%s
",add)
return
0
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)