#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int mygetline(char **lineptr, int *n, FILE *fp)
{
if(*lineptr == NULL)
{
*lineptr = (char *)malloc(10);
*n += 10;
}
char c = 0;
int count = 0;
while((c = fgetc(fp)) != EOF)
{
if(c == '\n')
{
break;
}else
{
(*lineptr)[count] = c;
count++;
}
if(count == *n - 1)
{
*n += 10;
*lineptr = realloc(*lineptr, *n);
}
}
(*lineptr)[count] = '\0';
return count;
}
int main ()
{
FILE *fp = fopen("b.txt", "r+");//判断是否为空
FILE *fp1 = fopen("c.txt", "w+");
char *line = NULL;
int n = 0;
mygetline(&line, &n, fp);
int len = strlen(line);
fwrite(line, len, 1, fp1);
close(fp);
close(fp1);
if(line)
{
free(line);
line = NULL;
}
return 0;
}