int main() { struct stud stu[10]; readrec(stu); writerec(stu); return 0; }
getrec(uchar *buf) /* get next input record from whatever source */ { /* note: tests whether buf == record */ int c; static int firsttime = 1; if (firsttime) { firsttime = 0; initgetrec(); } dprintf( ("RS=<%s>, FS=<%s>, ARGC=%g, FILENAME=%s\n", *RS, *FS, *ARGC, *FILENAME) ); donefld = 0; donerec = 1; buf[0] = 0; while (argno < *ARGC || infile == stdin) { dprintf( ("argno=%d, file=|%s|\n", argno, file) ); if (infile == NULL) { /* have to open a new file */ file = getargv(argno); if (*file == '\0') { /* it's been zapped */ argno++; continue; } if (isclvar(file)) { /* a var=value arg */ setclvar(file); argno++; continue; } *FILENAME = file; dprintf( ("opening file %s\n", file) ); if (*file == '-' && *(file+1) == '\0') infile = stdin; else if ((infile = fopen((char *)file, "r")) == NULL) ERROR "can't open file %s", file FATAL; setfval(fnrloc, 0.0); } c = readrec(buf, recsize, infile); if (c != 0 || buf[0] != '\0') { /* normal record */ if (buf == record) { if (!(recloc->tval & DONTFREE)) xfree(recloc->sval); recloc->sval = record; recloc->tval = REC | STR | DONTFREE; if (isnumber(recloc->sval)) { recloc->fval = atof(recloc->sval); recloc->tval |= NUM; } } setfval(nrloc, nrloc->fval+1); setfval(fnrloc, fnrloc->fval+1); return 1; } /* EOF arrived on this file; set up next */ if (infile != stdin) fclose(infile); infile = NULL; argno++; } return 0; /* true end of file */ }
node_t* readrec(FILE* fp){ node_t* root = malloc(sizeof(node_t)); if(fscanf(fp,"%d",&root->split)==EOF){ fprintf(stderr,"corrupt input file\n"); exit(1); } if(root->split >= 0){ fscanf(fp,"%g",&(root->threshold)); root->left=readrec(fp); root->right=readrec(fp); } else{ fscanf(fp,"%g%g",&root->pos, &root->neg); root->left=root->right=NULL; } return root; }
int main(void) { int n=10; struct student stu[10]; readrec( stu,n); writerec( stu,n); return 0; }
int main(int argc, char *argv[]) { link head; head = readrec(head, 10); writerec(head); free(head); return 0; }
int main(void) { stu st[STULEN]; // printf("%d\n", sizeof(st)); writerec(st, sizeof(st)/sizeof(st[0])); readrec(st, sizeof(st)/sizeof(st[0])); return 0; }
int main(int argc, char *argv[]) { struct stud arr[N]; readrec(arr); writerec(arr); return 0; }
Cell *awkgetline(Node **a, int n) /* get next line from specific input */ { /* a[0] is variable, a[1] is operator, a[2] is filename */ Cell *r, *x; extern Cell **fldtab; FILE *fp; char *buf; int bufsize = recsize; int mode; if ((buf = (char *) malloc(bufsize)) == NULL) FATAL("out of memory in getline"); fflush(stdout); /* in case someone is waiting for a prompt */ r = gettemp(); if (a[1] != NULL) { /* getline < file */ x = execute(a[2]); /* filename */ mode = ptoi(a[1]); if (mode == '|') /* input pipe */ mode = LE; /* arbitrary flag */ fp = openfile(mode, getsval(x)); tempfree(x); if (fp == NULL) n = -1; else n = readrec(&buf, &bufsize, fp); if (n <= 0) { ; } else if (a[0] != NULL) { /* getline var <file */ x = execute(a[0]); setsval(x, buf); tempfree(x); } else { /* getline <file */ setsval(fldtab[0], buf); if (is_number(fldtab[0]->sval)) { fldtab[0]->fval = atof(fldtab[0]->sval); fldtab[0]->tval |= NUM; } } } else { /* bare getline; use current input */ if (a[0] == NULL) /* getline */ n = getrec(&record, &recsize, 1); else { /* getline var */ n = getrec(&buf, &bufsize, 0); x = execute(a[0]); setsval(x, buf); tempfree(x); } } setfval(r, (Awkfloat) n); free(buf); return r; }
Cell *getline(Node **a, int n) /* get next line from specific input */ { /* a[0] is variable, a[1] is operator, a[2] is filename */ Cell *r, *x; char buf[RECSIZE]; FILE *fp; fflush(stdout); /* in case someone is waiting for a prompt */ r = gettemp(); if (a[1] != NULL) { /* getline < file */ x = execute(a[2]); /* filename */ if ((int) a[1] == '|') /* input pipe */ a[1] = (Node *) LE; /* arbitrary flag */ fp = openfile((int) a[1], getsval(x)); tempfree(x); if (fp == NULL) n = -1; else n = readrec(buf, sizeof(buf), fp); if (n <= 0) { ; } else if (a[0] != NULL) { /* getline var <file */ setsval(execute(a[0]), buf); } else { /* getline <file */ if (!(recloc->tval & DONTFREE)) xfree(recloc->sval); strcpy(record, buf); recloc->sval = record; recloc->tval = REC | STR | DONTFREE; if (isnumber(recloc->sval)) { recloc->fval = atof(recloc->sval); recloc->tval |= NUM; } donerec = 1; donefld = 0; } } else { /* bare getline; use current input */ if (a[0] == NULL) /* getline */ n = getrec(record); else { /* getline var */ n = getrec(buf); setsval(execute(a[0]), buf); } } setfval(r, (Awkfloat) n); return r; }
int main(void) { struct stud{ unsigned short id; char name[10]; int s[4]; double ave; }stu[N]; stu[0]->id=100; printf("%d\n", stu[0]->id); #if 0 readrec(stu); writerec(stu); #endif return 0; }
// Fileread reads jobs from f->path into list. // It returns 0 on success, or 1 if any errors occurred. int fileread(File *f, job list) { int err = 0, v; if (!readfull(f, &v, sizeof(v), &err, "version")) { return err; } switch (v) { case Walver: fileincref(f); while (readrec(f, list, &err)); filedecref(f); return err; case Walver5: fileincref(f); while (readrec5(f, list, &err)); filedecref(f); return err; } warnx("%s: unknown version: %d", f->path, v); return 1; }
int getrec(char **pbuf, int *pbufsize, int isrecord) /* get next input record */ { /* note: cares whether buf == record */ int c; static int firsttime = 1; char *buf = *pbuf; int bufsize = *pbufsize; if (firsttime) { firsttime = 0; initgetrec(); } dprintf( ("RS=<%s>, FS=<%s>, ARGC=%g, FILENAME=%s\n", *RS, *FS, *ARGC, *FILENAME) ); if (isrecord) { donefld = 0; donerec = 1; } buf[0] = 0; while (argno < *ARGC || infile == stdin) { dprintf( ("argno=%d, file=|%s|\n", argno, file) ); if (infile == NULL) { /* have to open a new file */ file = getargv(argno); if (*file == '\0') { /* it's been zapped */ argno++; continue; } if (isclvar(file)) { /* a var=value arg */ setclvar(file); argno++; continue; } *FILENAME = file; dprintf( ("opening file %s\n", file) ); if (*file == '-' && *(file+1) == '\0') infile = stdin; else if ((infile = fopen(file, "r")) == NULL) FATAL("can't open file %s", file); setfval(fnrloc, 0.0); } c = readrec(&buf, &bufsize, infile); if (c != 0 || buf[0] != '\0') { /* normal record */ if (isrecord) { if (freeable(fldtab[0])) xfree(fldtab[0]->sval); fldtab[0]->sval = buf; /* buf == record */ fldtab[0]->tval = REC | STR | DONTFREE; if (is_number(fldtab[0]->sval)) { fldtab[0]->fval = atof(fldtab[0]->sval); fldtab[0]->tval |= NUM; } } setfval(nrloc, nrloc->fval+1); setfval(fnrloc, fnrloc->fval+1); *pbuf = buf; *pbufsize = bufsize; return 1; } /* EOF arrived on this file; set up next */ if (infile != stdin) fclose(infile); infile = NULL; argno++; } *pbuf = buf; *pbufsize = bufsize; return 0; /* true end of file */ }
void readTree(FILE* fp, node_t** t){ *t=readrec(fp); }
int getrec(unsigned char **buf, int *bufsize) { int c, saved; static int firsttime = 1; if (firsttime) { firsttime = 0; initgetrec(); } dprintf( ("RS=<%s>, FS=<%s>, ARGC=%d, FILENAME=%s\n", *RS ? *RS : tostring(""), *FS ? *FS : tostring(""), (int) *ARGC, *FILENAME ? *FILENAME : tostring("")) ); donefld = 0; donerec = 1; if (*bufsize == 0) { if ((*buf = malloc(*bufsize = CHUNK)) == NULL) error(MM_ERROR, outofspace, "getrec"); **buf = '\0'; } saved = (*buf)[0]; (*buf)[0] = 0; while (argno < *ARGC || infile == stdin) { dprintf( ("argno=%d, file=|%s|\n", argno, file) ) ; if (infile == NULL) { /* have to open a new file */ file = getargv(argno); if (*file == '\0') { /* it's been zapped */ argno++; continue; } if (isclvar(file)) { /* a var=value arg */ setclvar(file); argno++; continue; } *FILENAME = file; dprintf( ("opening file %s\n", file) ); if (*file == '-' && *(file+1) == '\0') infile = stdin; else if ((infile = fopen((char *)file, "r")) == NULL) error(MM_ERROR, badopen, file, strerror(errno)); setfval(fnrloc, 0.0); } c = readrec(buf, bufsize, infile); if (c != 0 || (*buf)[0] != '\0') { /* normal record */ if (*buf == record) { if (!(recloc->tval & DONTFREE)) xfree(recloc->sval); recloc->sval = record; recloc->tval = REC | STR | DONTFREE; (void)is2number(0, recloc); } setfval(nrloc, nrloc->fval+1); setfval(fnrloc, fnrloc->fval+1); return 1; } /* EOF arrived on this file; set up next */ if (infile != stdin) fclose(infile); infile = NULL; argno++; } /* * POSIX.2 requires that NF stick with its last value * at the start of the END code. The most straightforward * way to do this is to restore the contents of record * [==buf when called from program()] so that getnf() will * recompute the same NF value unless something strange * occurs. This has the side effect of $0...$NF *also* * having sticky values into END, but that seems to match * the spirit of POSIX.2's rule for NF. */ if (posix) (*buf)[0] = saved; return 0; /* true end of file */ }