phoenixusa
Technical User
Hi there!
I am trying to get this puzzle out of this program for days now. Everytime I run the program, it reads the input file which has 21 lines. The lines are stored in a buffer of size 10. At the 11th line, it goes back to buffer [0], starting over, a circular buffer. However, at line 16 the program crashes and a core dumped is created. So any suggestion would be appreciated. Here is the code so far:
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000
#define MAX_BUFFER 10
char *ptr_array[MAX_BUFFER];
void insert_line(char *);
int getline(char s[], int lim);
void print_lines(char** ptr);
main (int argc, char *argv[ ])
{
int c, number = 0;
int i, optn;
char * ptr;
char line[MAXLINE];
char * temp_ptr_array[MAX_BUFFER];
/* code to enter data */
while(getline(line, MAXLINE)){
ptr = (char *) malloc(strlen(line +1));
printf("%p\n", ptr);
strcpy(ptr, line);
insert_line(ptr);
}
/* print customer data */
for (i = 0; i < MAX_BUFFER; i++)
temp_ptr_array = ptr_array;
print_lines(temp_ptr_array);
}
/* function to update the buffer with new customer data */
void insert_line(char * y)
{
static int new_index;
if(ptr_array[new_index])
{
free(ptr_array[new_index]);
ptr_array[new_index] = NULL;
}
ptr_array[new_index] = y;
new_index ++;
printf("%d\n", new_index);
if (new_index >= MAX_BUFFER)
new_index = 0;
}
/* function to print data */
void print_lines(char **ptr)
{
int i,j;
char * temp;
for (i = 0; i < MAX_BUFFER && ptr != NULL ; i++)
printf("%s", ptr);
}
/* function to get a line */
int getline(char s[], int lim)
{
int c, i;
i = 0;
while(--lim > 0 && (c=getchar()) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s = '\0';
return i;
}
I am trying to get this puzzle out of this program for days now. Everytime I run the program, it reads the input file which has 21 lines. The lines are stored in a buffer of size 10. At the 11th line, it goes back to buffer [0], starting over, a circular buffer. However, at line 16 the program crashes and a core dumped is created. So any suggestion would be appreciated. Here is the code so far:
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000
#define MAX_BUFFER 10
char *ptr_array[MAX_BUFFER];
void insert_line(char *);
int getline(char s[], int lim);
void print_lines(char** ptr);
main (int argc, char *argv[ ])
{
int c, number = 0;
int i, optn;
char * ptr;
char line[MAXLINE];
char * temp_ptr_array[MAX_BUFFER];
/* code to enter data */
while(getline(line, MAXLINE)){
ptr = (char *) malloc(strlen(line +1));
printf("%p\n", ptr);
strcpy(ptr, line);
insert_line(ptr);
}
/* print customer data */
for (i = 0; i < MAX_BUFFER; i++)
temp_ptr_array = ptr_array;
print_lines(temp_ptr_array);
}
/* function to update the buffer with new customer data */
void insert_line(char * y)
{
static int new_index;
if(ptr_array[new_index])
{
free(ptr_array[new_index]);
ptr_array[new_index] = NULL;
}
ptr_array[new_index] = y;
new_index ++;
printf("%d\n", new_index);
if (new_index >= MAX_BUFFER)
new_index = 0;
}
/* function to print data */
void print_lines(char **ptr)
{
int i,j;
char * temp;
for (i = 0; i < MAX_BUFFER && ptr != NULL ; i++)
printf("%s", ptr);
}
/* function to get a line */
int getline(char s[], int lim)
{
int c, i;
i = 0;
while(--lim > 0 && (c=getchar()) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s = '\0';
return i;
}