1

So I need to write a code with this compilation flags:

gcc -ansi -pedantic -Wall

All on Linux based OS. The output program should print its own source code. If I change the output file name AND there is a C file with the same name in the folder it would print its content.

Up until now I managed to pull this:

#include <stdio.h>
int main()
{
    char c; 
    FILE *my_file = fopen(__FILE__, "r");
    while (c != EOF)
    {
        c = fgetc(my_file);
        putchar(c);
    }
    fclose(my_file);
    return 0;
}

But if I change the file name and the C file name it gives me an error. Example: For files: prnt.c, prnt

Consule -> os/23$ ./prnt
#include <stdio.h>
int main()
{
    char c; 
    FILE *my_file = fopen(__FILE__, "r");
    while (c != EOF)
    {
        c = fgetc(my_file);
        putchar(c);
    }
    fclose(my_file);
    return 0;
}

for same file after I change the name to test.c, test (with out compilation)

/os/23$ ./test 
Segmentation fault (core dumped)

It is happening because of the __ FILE __ macro, it changes after compilation to the original file name. The question is how do I solve this problem?

9
  • 1
    First: int c; ... then check the return value of fopen(): ie if (my_file == NULL) { perror(__FILE__); exit(EXIT_FAILURE); } Commented Jun 5, 2021 at 10:04
  • I don't know exactly what you mean by "change the output file name", but I expect you need to look at argv[0] for the name the binary was executed as on the command line. Commented Jun 5, 2021 at 10:10
  • Hm, the source file would need to be part of the final executable. You might rely on the source residing parallelly to the executable and cut away the path from __FILE__. Commented Jun 5, 2021 at 10:36
  • while (c != EOF) is undefined behaviour, by the way, as c is not initialised! You might initialise to ~EOF to make assure it being different from. And it should be of type int to guarantee a byte 0xff really differ from EOF (apparently your char is signed anyway, otherwise you'd end up in an endless loop!). Commented Jun 5, 2021 at 10:41
  • 1
    You probably misunderstand the assignment. See en.m.wikipedia.org/wiki/Quine_(computing) Commented Jun 5, 2021 at 15:15

1 Answer 1

1

So I checked into Paul Hankin's suggestion, I managed to solve the problem. Hope that in the future it will help somebody.

My Solution:

#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
    /*
    This program prints the source code
    of the 'C' file with the same name in the
    same directory.
    */
    char c = 'a'; /* Initialization before while loop.*/
    char* locat = argv[0]; /* The path to the program file.*/
    char suff[3] = ".c\0";
    FILE* my_file;

    strcat(locat, suff); /* Doesn't work for Windows.*/

    my_file = fopen(locat, "r");
    while (c != EOF)
    {
        c = fgetc(my_file);
        putchar(c);
    }
    fclose(my_file);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.