Saturday, June 23, 2018

c - Linked-List file input, cant stop with EOF

I want to read from a file with ./sample < input.txt command but it does not stop with EOF.
I need to ctrl+c to determine that it is EOF.
So how can I read more than one line?
I need to put it into the main loop which can determine EOF without ctrl+c.
As I planned, there must be 2 loops:
Main loop: to get other lines from file, Insert new node to fill up condition should be "EOF".
Sub loop: to fill up character array by characters, condition should be "\n".



#include 
#include


#define SIZE 80


struct node {
char str[SIZE];
struct node* next;
};

void read_line(struct node* line_list);


int main(void)
{

struct node* line_list = NULL; // linked list - headp

read_line(line_list);

return 0;
}


void read_line(struct node* line_list)
{
int ch, i = 0;

struct node* temp = malloc(sizeof(struct node));
if(temp!=NULL) {
while((ch = getchar()) != EOF){
if (ch != '\n')
{

temp->str[i] = '\0';
//insert(&line_list,temp.str);
break;
}
if (i < SIZE)
temp->str[i++] = ch;
printf("New str: %s\n",temp->str);
}
}
}

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...