Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
How do I wait on output from sysfs?

I tried the obvious (see below), but it does not catch new output to /var/log/syslog. I'm sure there is an answer somewhere already, but I have not been able to find it.

Here is my code (my best guess how to do this):

#include <stdio.h>
#include <stdlib.h>  // provides fopen()
#include <unistd.h>  // provides sleep()


int main() {

    // *** This is failing to pick up new output on /var/log/syslog. I'm not sure
    // how to do this properly.

    // Open a read file handle on /sys/kernel/tracing/trace and wait for data to
    // appear there. When it does, echo it to the screen. This is essentially an
    // implementation of "tail -f /sys/kernel/tracing/trace".

    //FILE *fp = fopen("/sys/kernel/tracing/trace", "r");
    FILE *fp = fopen("/var/log/syslog", "r");
    char c;

    if (fp != NULL) {
        printf("Opened the file successfully. Waiting...\n");
    } else {
        printf("Failed to open the file.\n");
      exit(1);
    }

    // Check every second and output whatever is in the buffer.
    while(1) {
        c = fgetc(fp);

        // We get back -1 when there is nothing to read.
        if (c != -1) {
            printf("%c", c);
        } else {
            printf(".");  fflush(stdout);
            sleep(1);
        }
    }

    fclose(fp);
    return 0;
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You need to call clearerr(fp) to clear the feof indicator:

// Check every second and output whatever is in the buffer.
while(1) {
    c = fgetc(fp);

    // We get back -1 when there is nothing to read.
    if (c != -1) {
        printf("%c", c);
    } else {
        printf(".");  fflush(stdout); 
        if(feof(fp)) clearerr(fp);  // <-- clear the feof indicator
        sleep(1);
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda