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;
}
My Code so far:
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");