Search code examples
cloopsprogram-entry-point

Main function stuck in a loop without it entering one


I have an assignment which is to make a basic implementation of the tail command in Linux, and it was going well, but when i run it it just goes to the next line and does nothing. Here is the code:

int main(int argc, char *argv[])
{
    int counter;
    printf("program started");
    if(argc == 1)
    {
        printf("hello1");
        tailSTDIN();
    }
    else if(argc == 2)
    {
        printf("here");
        tailFile(argv[1]);
        printf("tailed");
    }
    else for(counter = 0; counter < argc; counter++)
    {
        printf("loop");
        if(argv[argc] == "-")
        {
            printTitle("standard input");
            tailSTDIN();
        }
        else
        {
            printTitle(argv[counter]);
            tailFile(argv[counter]);
        }
    }

    return 0;
}

Ok, so the problem is that it just gets stuck, it doesn't even get to the first printf(), where it says program started, it just waits for something. And that's regardless of how many or what arguments I give it. If you believe you need the full code with the rest of the functions, I'll put it in, but the issue is that it never gets to the point of calling them.


Solution

  • When you use stdout for output lines are buffered until whole buffer is filled or newline character is encountered. Terminate you line with \n character:

    printf("program started\n");
    

    and you'll get your output.