And after asking twice, I notice that I still haven't answered your question, sorry:
I was just curious as to why you decided to make one static and the other one not.
My implementation comes from this FAQ without fully understanding it.
Turns out I'm wrong. The below code produces an "interesting" compiler output:
Code:
heras@vhbh:~/code/c/readfile$ gcc -Wall readfile.c open_file.c read_line.c -o read
open_file.h:1: warning: ‘open_file’ used but never defined
open_file.c:7: warning: ‘open_file’ defined but not used
/tmp/ccMsUG3J.o: In function `main':
readfile.c:(.text+0x61): undefined reference to `open_file'
collect2: ld returned 1 exit status
Code:
/*
readfile2.3.c
*/
#include <stdio.h>
#include <stdlib.h>
#include "open_file.h"
#include "read_line.h"
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "%s usage: <address>\n", argv[0]);
exit (EXIT_FAILURE);
}
FILE *open_fd;
open_fd = open_file(argv[1], "r");
int line_length = 73;
char *line_pointer;
line_pointer = (char *)malloc(line_length);
while (read_line(open_fd, line_length, line_pointer) != EOF) {
printf("%s\n", line_pointer); //FIXME: does not print last line
}
free(line_pointer);
return (EXIT_SUCCESS);
}
open_file.h
Code:
static FILE *open_file(char *, char *);
open_file.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "open_file.h"
static FILE *open_file(char *file, char *mode)
{
FILE *fp = fopen(file, mode);
if (fp == NULL) {
perror("fopen");
exit (EXIT_FAILURE); //FIXME: return failure
}
return (fp);
}
read_line.h
Code:
int read_line(FILE *, int, char *);
read_line.c
Code:
#include <stdio.h>
#include <ctype.h>
#include "read_line.h"
//FIXME: static int read_line() ?
int read_line(FILE *file, int line_length, char *line_pointer)
{
int i = 0, c;
while ((c = fgetc(file) ) != EOF) {
if (isalnum(c) != 0 || ispunct(c) != 0 || c == ' ') {
*line_pointer = c;
line_pointer++;
}
if (i == (line_length - 2)) {
break;
}
i++;
}
*line_pointer = '\0';
return (c);
}
Removing static from both open_file.h and open_file.c makes the "interesting" part go away.
Note1 that some of the badness has not been fixed, but it will be.
Note2 that matsp rules (together with all the other helpfull folks in these parts).
ps:I don't have a question about this code, it's just a follow-up.