Back

#include<stdio.h>
#include<string.h>

#define SIZE 1000

char *fgets1(char *s, int n, FILE *iop);
int fputs1(char *s, FILE *iop);
int getline(char *line, int max);
char line[SIZE];

int main(int argc, char *argv[])
{
 FILE *iop,*iop2;
 int n=0;
 iop = fopen("test2.c", "r");
 iop2 = fopen("test3.c","w");
 while(fgets1(line,SIZE,iop))
  {
    printf("%s",line);
    fputs1(line, iop2);
  }
fclose(iop);
fclose(iop2);

 while((n=getline(line,SIZE))>0)
  printf("Length:%d\n",n);

return 0;
}

char *fgets1(char *s, int n, FILE *iop)
 {
   register int c;
   register char *cs;

   cs = s;
   while (--n > 0 && (c=getc(iop)) != EOF)
    if ((*cs++ = c) == '\n')
      break;
    *cs = '\0';
   return (c==EOF && cs==s) ? NULL : s;
 }

int fputs1(char *s, FILE *iop)
 {
   int c;

   while (c = *s++)
    putc(c, iop);
   return ferror(iop) ? EOF : 0;
 }

int getline(char *line, int max)
 {
    if (fgets1(line, max, stdin) == NULL)
     return 0;
    else
     return strlen(line);
 }

Top