Chad said:
say my input file is
[cdalten@localhost ~]$ more suck
______
< gnu? >
------
\ , ,
\ /( )`
\ \ \___ / |
/- _ `-/ '
(/\/ \ \ /\
/ / | ` \
O O ) / |
`-^--'`< '
(_.) _ ) /
`.___/` /
`-----' /
<----. __ / __ \
<----|====O)))==) \) /====
<----' `--' `.__,' \
| |
\ /
______( (_ / \______
,' ,-----' | \
`--{__________) \/
[cdalten@localhost ~]$
What I want to do is read this file in, store it in some kind of array
and then be able to print this on stdout. I thought maybe i could do
something like the following
read in each line of the art via fgets()
store each line in an array
repeat
now print the array that holds these lines
My preference for storing lines of a text file, is in a linked list.
I don't understand why you want to store the file contents
in memory prior to displaying it.
Without the storage, the whole program boils down to new.c
/* BEGIN new.c */
#include <stdio.h>
int main(void)
{
int c;
FILE *fp = fopen("file.txt", "r");
if (fp == NULL) {
puts("fp == NULL");
} else {
while ((c = getc(fp)) != EOF) {
putchar(c);
}
fclose(fp);
putchar('\n');
}
return 0;
}
/* END new.c */
type_1.c shows one way to read whole text files
into a linked list and display them.
/* BEGIN type_1.c */
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#define INITIAL_BUFFER_SIZE 0
#define ARGV_0 "type_1"
struct list_node {
struct list_node *next;
void *data;
};
int get_line(char **lineptr, size_t *n, FILE *stream);
void list_free(struct list_node *node, void (*free_data)(void *));
int list_fputs(struct list_node *node, FILE *stream);
struct list_node *string_node(struct list_node **head,
struct list_node *tail,
char *data);
int main(int argc, char *argv[])
{
int rc;
FILE *fd;
char *buff_ptr;
size_t buff_size;
struct list_node *head, *tail;
if (argc > 1) {
buff_size = INITIAL_BUFFER_SIZE;
buff_ptr = malloc(buff_size);
if (buff_ptr == NULL && buff_size != 0) {
printf("malloc(%lu) == NULL\n", (long unsigned)buff_size);
exit(EXIT_FAILURE);
}
tail = head = NULL;
while (*++argv != NULL) {
fd = fopen(*argv, "r");
if (fd != NULL) {
while ((rc = get_line(&buff_ptr, &buff_size, fd))
{
tail = string_node(&head, tail, buff_ptr);
if (tail == NULL) {
break;
}
}
fclose(fd);
switch (rc) {
case EOF:
if (buff_ptr != NULL
&& strlen(buff_ptr) != 0)
{
puts("rc equals EOF\n"
"The string in buff_ptr is:");
puts(buff_ptr);
tail = string_node(&head, tail, buff_ptr);
}
break;
case 0:
puts("realloc returned a null pointer "
"value in line_to_string.");
if (buff_size > 1) {
puts("rc equals 0\n"
"The string in buff_ptr is:");
puts(buff_ptr);
tail = string_node(&head, tail, buff_ptr);
}
break;
default:
puts("malloc problem in string_node.");
break;
}
} else {
printf("\nfopen() problem with \"%s\"\n", *argv);
break;
}
list_fputs(head, stdout);
list_free(head, free);
head = NULL;
}
free(buff_ptr);
} else {
puts("Usage:\n>" ARGV_0
" <TEXT_FILE_0> <TEXT_FILE_1> <TEXT_FILE_2> ...\n");
}
return 0;
}
int get_line(char **lineptr, size_t *n, FILE *stream)
{
int rc;
void *p;
size_t count;
count = 0;
while ((rc = getc(stream)) != EOF) {
if (count != -1) {
++count;
}
if (count + 2 > *n) {
p = realloc(*lineptr, count + 2);
if (p == NULL) {
if (*n > count) {
(*lineptr)[count] = '\0';
(*lineptr)[count - 1] = (char)rc;
} else {
if (*n != 0) {
**lineptr = '\0';
}
ungetc(rc, stream);
}
count = 0;
break;
}
*lineptr = p;
*n = count + 2;
}
if (rc == '\n') {
(*lineptr)[count - 1] = '\0';
break;
}
(*lineptr)[count - 1] = (char)rc;
}
if (rc != EOF) {
rc = count > INT_MAX ? INT_MAX : count;
} else {
if (*n > count) {
(*lineptr)[count] = '\0';
}
}
return rc;
}
void list_free(struct list_node *node, void (*free_data)(void *))
{
struct list_node *next_node;
while (node != NULL) {
next_node = node -> next;
free_data(node -> data);
free(node);
node = next_node;
}
}
int list_fputs(struct list_node *node, FILE *stream)
{
while (node != NULL) {
if (fputs(node -> data, stream) == EOF) {
return EOF;
}
if (putc('\n', stream) == EOF) {
return EOF;
}
node = node -> next;
}
return '\n';
}
struct list_node *string_node(struct list_node **head,
struct list_node *tail,
char *data)
{
struct list_node *node;
node = malloc(sizeof *node);
if (node != NULL) {
node -> next = NULL;
node -> data = malloc(strlen(data) + 1);
if (node -> data != NULL) {
strcpy(node -> data, data);
if (*head == NULL) {
*head = node;
} else {
tail -> next = node;
}
} else {
free(node);
node = NULL;
}
}
return node;
}
/* END type_1.c */