Commit b0667bc1 authored by kjc015's avatar kjc015
Browse files

Exercise 4 Complete

parent 46a60dce
all: head2 head3
CC=gcc
CFLAGS=-Wall -Werror
head2: head2.o fileio.o
$(CC) $(CFLAGS) head2.o fileio.o -o head2
%.o: %.c
$(COMPILE.c) $(OUTPUT_OPTION) $<
head3: head3.o fileio.o
$(CC) $(CFLAS) head3.o fileio.o -o head3
......@@ -20,4 +20,17 @@ int read_file_bytes (char* filename, int bytes, char* buffer)
}
int read_file_lines (char* filename, char buffer[MAXLINES][MAXBYTES+1])
{
FILE* opn = fopen(filename, "r");
char* pointer = fgets(buffer[0], MAXBYTES, opn);
int i = 0;
while (pointer != NULL && i < MAXLINES){
i++;
pointer = fgets(buffer[i], MAXBYTES, opn);
}
return i;
}
......@@ -9,8 +9,11 @@
#include <string.h>
#define MAXBYTES 1024
#define MAXLINES 10
int read_file_bytes (char* filename, int bytes, char* buffer);
int read_file_lines (char* filename, char buffer[MAXLINES][MAXBYTES+1]);
#endif
/* Kyle Chrysler
* Tuesday 1:20
* lab 04 - head.c
* compile with: head
* notes: none
*/
// for O_RDONLY, etc.
#include "fileio.h"
#define MAXBYTES 1024
#define MAXLINES 10
int main(int argc, char* argv[])
{
int readbytes = 10; // default to 10 bytes
char buffer[MAXLINES][MAXBYTES+1]; // buffer to hold file data
if (argc < 2){
// missing required argument
printf("Usage %s <filename> [<bytes>]\n",
argv[0]);
exit(-1);
}
if (argc > 2){
// process optional bytes argument
readbytes = atoi(argv[2]);
if (readbytes > MAXBYTES){
printf("bytes is too large, max supported value is %d!\n",
MAXBYTES);
exit(-2);
}
}
int var = read_file_lines(argv[1], buffer);
if (var >= 0){
int i;
for(i = 0; i < var-1; i++){
printf("%s\n", buffer[i]);
}
} else {
printf("Error: could not read file!\n");
exit(-3);
}
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment