int countlines(FILE *fp){ int nlcount = 0; int ichar; myrewind(fp); while( (ichar=fgetc(fp)) != EOF ){ if( ichar == '\n' ){ nlcount ++ ;} } myrewind(fp); return(nlcount); }
/* measures the number of columns of the first line */ int countcolumns(FILE *fp){ char *line; myrewind(fp); line= getline(fp); return( counttokens( line) ); }
BOOLEAN checkfile(FILE *fp){ char *line; int nold, nnew; int linecount; BOOLEAN kosher = TRUE; myrewind(fp); /* read in the first line */ line= getline(fp); linecount =1; if( feof(fp) ){ return; /*empty file */ }else{ nold = counttokens(line); #ifdef DEBUG /* printf("%d:\t%s", nold, line); */ #endif } line = getline(fp); while( !feof(fp) ){ linecount ++; nnew = counttokens(line); #ifdef DEBUG /* printf("%d:\t%s", nnew, line); */ #endif if( nnew != nold){ kosher = FALSE; fprintf(stderr, "line %d has %d tokens; line %d has %d\n", 1, nold, linecount, nnew); } /* assert( nnew == nold ); */ line = getline(fp); } myrewind(fp); return (kosher); }
int main(void) { MYFILE *fp; char path[1024]; char input[1024]; char output[1024]; size_t ret; int len; printf("bufsize: %d\n", BUFSIZ); printf("pls input a path: "); my_fgets(path, 1024); printf("pls input a str: "); my_fgets(input, 1024); len = strlen(input); printf("len: %d\n", len); fp = myopen(path, "w+"); ret = mywrite(input, 1, len, fp); printf("write ret: %d\n", ret); myrewind(fp); myread(output, 1, len, fp); output[len] = '\0'; printf("read ret: %d\n", ret); printf("%s\n", output); myclose(fp); return 0; }