I have to make a program for my topics class that is a "poor mans" variation to the unix sort command. Program is suppose to read a filename in as a command line argument. We are suppose to use the malloc() function in this program. also a requirement is :
Re-implement the readLine() function so that it no longer makes use of fgets(). Instead, process the input on a given line character-by-character. I can post the code of this readLine() function also. I'm really confused on where to go from here, and also how I am even suppose to rewrite this readLine() function w/o the fgets(). Heres my code so far, and the function were suppose to change:
readLine():
My Code so far:Code:/* Read and store one line of input return NULL if stream is at eof or there was a read error otherwise returns the pointer s to the char array reads and stores up to n characters in the char array, stopping at newline if there isn't space to hold the entire line, overflow is set to true and any trailing characters on that line are discarded */ char *readLine(char *s, int n, FILE *fp, bool *overflow) { char *result; int length; int ch; int skipped; /* the number of NON-newline characters skipped over for lines too long to fit in the given char array */ result = fgets(s, n, fp); /* read a line */ if (result == NULL) { *overflow = false; return NULL; /* either stream is at eof or there was a read error */ } /* read was successful: did we get the entire line? */ length = strlen(s); if (s[length - 1] == '\n') { s[length - 1] = '\0'; /* we don't want to store the newline */ *overflow = false; } else { /* skip to end of current line */ skipped = 0; while ((ch = getc(fp)) != '\n' && (ch != EOF)) skipped++; *overflow = (skipped > 0); } return s; }
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINES 1000 /* maximum number of reminders */ #define WORD_LENGTH 10 /* max length of reminder message */ int read_line(char str[], int n); int main(int argc, char *argv[]) { char *lines[MAX_LINES]; char ch, line = argv[1]; int day, i, j, num_lines = 0; for (;;) { if (num_lines == MAX_LINES) { printf("-- No space left --\n"); break; } printf("Enter name of file."); FILE *fp = fopen(argv[1], "r");



LinkBack URL
About LinkBacks


