Sunday, 28 August 2016

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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...