Monday 11 June 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...