编写一个业务函数,实现按行读取文件。把内容按照第三种内存模型打包数据传出,把行数通过函数参数传出。
函数原型有两个,任意选择其一
要求1:请自己任意选择一个接口(函数),并实现功能;70分
要求2:编写测试用例。30分
要求3:自己编写内存释放函数
*/
char **readFile1(const char *pfilename/*in*/, int *lineNum/*in out*/);
int readFile2(const char *pfilename/*in*/, char ***p/*out*/, int *lineNum/*int out*/);
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char **readFile1(const char *pfilename/*in*/, int *lineNum/*in out*/);
int readFile2(const char *pfilename/*in*/, char ***p/*out*/);
char **readFile1(const char *pfilename/*in*/, int *lineNum/*in out*/)
{
int rv = 0;
FILE *fp = NULL;
char lineBuf[1024 * 4];
char **pTmp = NULL;
char *p = NULL;
int tmpLine = 0, strLine = 0, i = 0;
if (pfilename == NULL || lineNum == NULL)
{
rv = -1;
printf("readFile1() err. param err \n");
goto End;
}
fp = fopen(pfilename, "r");
if (fp == NULL)
{
rv = -2;
printf("fopen() err. \n");
goto End;
}
while(!feof(fp))
{
memset(lineBuf, 0, sizeof(lineBuf));
p = fgets(lineBuf, 1024 * 4, fp);
if (p == NULL)
{
break;
}
else
{
tmpLine++;
}
}
pTmp = (char **)malloc(tmpLine * sizeof(char *));
if (pTmp == NULL)
{
rv = -2;
printf("malloc() err. \n");
goto End;
}
//让文件指针指向文件的开头,目的:第二次从头检索
fseek(fp, 0L, SEEK_SET);
i = 0;
while (!feof(fp))
{
//读每一行
memset(lineBuf, 0, sizeof(lineBuf));
p = fgets(lineBuf, 1024 * 4, fp);
if (p == NULL)
{
break;
}
strLine = strlen(lineBuf);
pTmp[i] = (char *)malloc((strLine + 1) * sizeof(char));
if (pTmp[i] == NULL)
{
rv = -3;
printf("malloc() err. \n");
goto End;
}
strcpy(pTmp[i], lineBuf);
i++;
}
End:
if (fp != NULL)
{
fclose(fp);
}
//赋值
*lineNum = tmpLine;
return pTmp;
}
void FreeMypp(char **p, int linenum)
{
int i = 0;
if (p == NULL)
{
return NULL;
}
for (i = 0; i<linenum; i++)
{
if (p[i] != NULL)
{
free(p[i]);
}
}
free(p);
return;
}
int readFile2(const char *pfilename/*in*/, char ***myfileP/*in out*/)
{
int rv = 0;
FILE *fp = NULL;
char lineBuf[1024 * 4];
char **pTmp = NULL;
char *p = NULL;
int tmpLine = 0, strLine = 0, i = 0;
if (pfilename == NULL || myfileP == NULL)
{
rv = -1;
printf("readFile1() err. param err \n");
goto End;
}
fp = fopen(pfilename, "r");
if (fp == NULL)
{
rv = -2;
printf("fopen() err. \n");
goto End;
}
//第一遍 读取文件有多少行
while (!feof(fp))
{
//读每一行
memset(lineBuf, 0, sizeof(lineBuf));
p = fgets(lineBuf, 1024 * 4, fp);
if (p == NULL)
{
break;
}
else
{
tmpLine++;
}
}
pTmp = (char **)malloc((tmpLine + 1) * sizeof(char *));
if (pTmp == NULL)
{
rv = -2;
printf("malloc() err. \n");
goto End;
}
memset(pTmp, 0, (tmpLine + 1) * sizeof(char *));
//让文件指针指向文件的开头,目的:第二次从头检索
fseek(fp, 0L, SEEK_SET);
//
i = 0;
while (!feof(fp))
{
//读每一行
memset(lineBuf, 0, sizeof(lineBuf));
p = fgets(lineBuf, 1024 * 4, fp);
if (p == NULL)
{
break;
}
strLine = strlen(lineBuf);
pTmp[i] = (char *)malloc((strLine + 1) * sizeof(char));
if (pTmp[i] == NULL)
{
rv = -3;
printf("malloc() err. \n");
goto End;
}
strcpy(pTmp[i], lineBuf);
i++;
}
End:
if (fp != NULL)
{
fclose(fp);
}
//赋值
//*lineNum = tmpLine;
*myfileP = pTmp;
return rv;
}
void FreeMypp3(char **p)
{
int i = 0;
if (p == NULL)
{
return NULL;
}
for (i = 0; p[i] != NULL; i++)
{
if (p[i] != NULL)
{
free(p[i]);
}
}
free(p);
return;
}
void main01()
{
int ret = 0;
int linenum = 0;
const char *pfilename = "D:\\test\\a.txt";
char **p = NULL;
int i = 0;
p = readFile1(pfilename/*in*/, &linenum/*in out*/);
if (p == NULL)
{
ret = -1;
printf("func err readFile1(pfilename/*in*/, &linenum/*in out*/); err :%d\n",ret);
}
for (i = 0; i<linenum; i++)
{
printf("%s\n", p[i]);
}
FreeMypp(p, linenum);
system("pause");
}
void main()
{
int ret = 0;
int linenum = 0;
const char *pfilename = "D:\\test\\a.txt";
char **p = NULL;
int i = 0;
ret = readFile2(pfilename/*in*/, &p/*out*/);
if (ret != 0)
{
return;
}
for (i = 0; p[i] != NULL; i++)
{
printf("%s\n", p[i]);
}
FreeMypp3(p);
system("pause");
}