2016-05-11 17:53:54 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2016-05-19 15:15:22 +03:00
|
|
|
char * fgets ( char * str, int num, FILE * file )
|
|
|
|
// need to ignore \r\n in text mode
|
2016-05-11 17:53:54 +03:00
|
|
|
{
|
|
|
|
int rd = 0;
|
|
|
|
char c;
|
2016-05-19 15:15:22 +03:00
|
|
|
|
|
|
|
if(!file || !str)
|
|
|
|
{
|
|
|
|
errno = E_INVALIDPTR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (rd < num - 1)
|
2016-05-11 17:53:54 +03:00
|
|
|
{
|
2016-05-19 15:15:22 +03:00
|
|
|
c = fgetc(file);
|
2016-05-11 17:53:54 +03:00
|
|
|
if (EOF == c) break;
|
|
|
|
if ('\n' == c)
|
|
|
|
{
|
|
|
|
str[rd++] = c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
str[rd++] = c;
|
|
|
|
}
|
|
|
|
if (0 == rd) return NULL;
|
2016-05-19 15:15:22 +03:00
|
|
|
else
|
|
|
|
{
|
2016-05-11 17:53:54 +03:00
|
|
|
str[rd] = '\0';
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|