0

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>


Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • `fflush()` has no effect on *what* is printed (barring abrupt termination of the program). It affects only *when* the output is pushed out of the program to wherever it is going -- the standard output in this case. Thus, what you describe does not indicate any issue with `fflush()`, but rather one with your own code preceding the `fflush()` call. – John Bollinger Mar 15 '22 at 16:25
  • @JohnBollinger But could you tell where abrupt termination takes place?, actually the above code was part of a much larger one, where everything except this part worked as expected – Karthik Raja Anandan Mar 15 '22 at 16:31
  • The fact that you *do* see the output corresponding to the `printf` following the `fflush` demonstrates that the issue is not arising from abrupt termination. But to answer the question, abrupt termination would be the result of the program being terminated by a signal or calling `_exit()` (not `exit()`) or `abort()`. – John Bollinger Mar 15 '22 at 16:35
  • 2
    As a general rule, if you program misbehaves then the problem is in your code, not the compiler or standard library. There are exceptions, but they are rare. – John Bollinger Mar 15 '22 at 16:40
  • 3
    The constant `EOF` might not be representable as a `char` (for example when `char` is an unsigned type and `EOF` is `(-1)`), so comparisons between `EOF` and `ch` (of type `char`) are not portable. – Ian Abbott Mar 15 '22 at 16:42
  • I get it that there might be a fault in my code, but everything is logically right, in the above code, try running the code here https://replit.com/@KarthikrajaA/Lexanalyzer#main.c – Karthik Raja Anandan Mar 15 '22 at 16:47
  • 1
    You can change `if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_' || (ch >= '0' && ch <= '9'))` --> `if (isalpha(ch) || isdigit(ch) || ch == '_')` https://linux.die.net/man/3/isalpha – yano Mar 15 '22 at 16:50
  • Perhaps everything *is* right in the code presented. When I compile and run it, I get output that **does** contain the `#include` directive: `ji#include -- preprocessor directive`. Voting to close as not reproducible. – John Bollinger Mar 15 '22 at 16:52

0 Answers0