CS140 -- Midterm Exam: Extra stuff


fields.h

#define MAXLEN 1001
#define MAXFIELDS 1000

typedef struct inputstruct {
  char *name;               /* File name */
  FILE *f;                  /* File descriptor */
  int line;                 /* Line number */
  char text1[MAXLEN];       /* The line */
  char text2[MAXLEN];       /* Working -- contains fields */
  int NF;                   /* Number of fields */
  char *fields[MAXFIELDS];  /* Pointers to fields */
  int file;                 /* 1 for file, 0 for popen */
} *IS;

extern IS new_inputstruct(/* FILENAME -- NULL for stdin */);
extern int get_line(/* IS */); /* returns NF, or -1 on EOF.  Does not
                                  close the file */
extern void jettison_inputstruct(/* IS */);  /* frees the IS and fcloses 
                                                the file */

int strlen(char *s);
Strlen() assumes that s is a null-terminated string. It returns the number of characters before the null character.
char *strcpy(char *s1, char *s2);
Strcpy() assumes that s2 is a null-terminated string, and that s1 is a (char *) with enough characters to hold s2, including the null character at the end. Strcpy() then copies s2 to s1. It also returns s1.
char *strdup(char *s);
Strdup() gives you a new copy of s that it has malloc()'d for you.
FILE *fopen(char *filename, char *mode);
Lets you use stdio functions like fprintf() and fscanf() to write to and read from files. Mode is "r" if you are opening the file for reading, and "w" if you opening the file for writing.