I need a code to read using the function read from stdin until there's a newline, and then use that line. The thing is that I can't just read chars and putting them in a array until I read "\n" cause the exercise requires to read a max of 16bytes at a time.
I tried using the function strtok to divide the lines using the divider "\n" but it doesn't work as I want, so is there any way to only read until it detects the "\n"? Also I need it to be constantly reading it.
Here is what I have right now:
void readline(char *buf, unsigned buf_size){
ssize_t num_read;
while((num_read = read(STDIN_FILENO,buf,buf_size)) > 0){
}
}
Then in the main function:
while(i == 0){
readline(buf,BUF_SIZE);
token1 = strtok(buf,"\n");
while(token1 != NULL){
orden->num=0;
token2 = strtok(token1, " ");
orden->args[orden->num] = token2;
while(token2 != NULL){
if(token2!=NULL) {
orden->num+=1;
orden->args[orden->num] = token2;
}
}
orden->num+=1;
orden->args[orden->num] = NULL;
insertar_final(lista,orden);
token1 = strtok(NULL,"\n");
};
...
The others strtoks declarations using " " are for other things my code needs, but that works so don't mind them.