fflush(stdout) not working but \n' works in C language

201 Views Asked by At

I was working on a homework problem when I encounter this error, fflush inside main function in main.c is not working I tried the code in ubuntu local system and in repl.it and the output does not contain "#include" line from sample.c , The only line "preprocessor " is printed in terminal.. Files used: main.c

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>

char token[1024];

int toksize = 0;
int breaker = 0;

int is_normal(char ch) {
    if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
            || ch == '_' || (ch >= '0' && ch <= '9'))
        return 1;
    return 0;
}

int Tokenizer(FILE * file_ptr) {
    char ch;
    toksize = 0;

    ch = 'a';
    while (is_normal(ch) == 1 && ch != EOF) {
        ch = fgetc(file_ptr);
        if (ch == EOF) break;
        if (toksize > 0 && !is_normal(ch)) break;
        token[toksize] = ch;
        toksize++;
        token[toksize] = '\0';
    }
    if (ch == EOF)
        breaker = 1;
    if (toksize > 1 || is_normal(token[0]))
        fseek(file_ptr, -1L, SEEK_CUR);
    // fflush(stdin);
    // fflush(stdout);
    return 1;
}

int main() {
    fprintf(stderr, "ji");
    int flag = 0;
    FILE * fd = fopen("sample.c", "r");
    breaker = 0;
    if (fd == NULL) {
        printf("file not found!!");
        return 0;
    }

    while (breaker == 0 && Tokenizer(fd) > 0) {
        int stage = 0;

        if (token[0] == '#') { // handling preprocessor
            while (token[0] != '\n') {
                printf("%s", token);
                if (Tokenizer(fd) == 0) {
                    breaker = 1;
                    break;
                }
            }
            fflush(stdout);
            printf(" -- preprocessor directive\n");
        }
    }
}

sample.c

#include<stdio.h>


0

There are 0 best solutions below