struct定义问题
struct FILELINE{
char DataLine[100];
struct FILELINE* next; // 这里什么意思不懂,这样是属于递归么?有啥作用
};
struct FILELINE* ReadFile2Memory( char *fileName )
{
struct FILELINE* p1 = NULL;
struct FILELINE* p2 = NULL;
struct FILELINE* head = NULL;
FILE* fp = NULL;
p1 = (struct FILELINE*)malloc( sizeof(struct FILELINE) ); //开辟新内存单元
if( p1 == NULL )
{
printf( "开辟新内存单元失败......\n" );
return( (struct FILELINE*)NULL );
}
fp = fopen( fileName , "r" );//打开文件句柄
if( fp == NULL )
{
printf( "打开文件%s失败......\n" , fileName );
return( (struct FILELINE*)NULL );
}
// 读取第一行,存入第一个节点
memset( p1, 0, sizeof(struct FILELINE) );
fgets( p1->DataLine, MAX_LINE_LENGTH, fp );
if( p1->DataLine[strlen(p1->DataLine)-1] == '\n' )
{
p1->DataLine[strlen(p1->DataLine)-1]='\0';
}
iCount++;
head = p1;
p2 = p1;
while( !feof(fp) )
{
p1 = (struct FILELINE*)malloc( sizeof(struct FILELINE) );
if( p1 == NULL )
{
printf( "开辟新内存单元失败......\n" );
break;
}
memset( p1, 0, sizeof(struct FILELINE) );
fgets( p1->DataLine, MAX_LINE_LENGTH, fp );
if( p1->DataLine[strlen(p1->DataLine)-1] == '\n' )
{
p1->DataLine[strlen(p1->DataLine)-1]='\0';
}
iCount++;
p2->next = p1; //这里不懂
p2 = p1;
}
p2->next = NULL;
fclose( fp );
return head;
}
[ 本帖最后由 OneMan 于 2009-10-30 23:59 编辑 ]