-1

I'm currently doing an exercise where I have to create a program that takes all the code that is written inside it, and outputs it to the screen when the program is executed.

The exercise suggests that we may find it appropriate to change the file names of the program in the future - and assuming that the renaming is done in a coordinated manner, i.e. the source file and the execution file are given the same new name (except for the extension), the program should work correctly, without the need for any changes to the source code, and without the need to recompile.

The C program itself is called 'prnt.c'-

I wrote the following code:

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

#define ENDFILEC ".c" /* extension  */

int main(int argc, char *argv[])
{
   FILE *filePointer;
   int character;
   char *fileNameToOpen;

   fileNameToOpen = (char *)malloc(strlen(argv[0]) + 3); /* allocating memory for string + 3 for the extension - '.c' and \0 */

   strcpy(fileNameToOpen, argv[0]); 
   strcat(fileNameToOpen , ENDFILEC); /* ending '.c' in the end */ 

   filePointer = fopen(fileNameToOpen, "r");
     while(!feof(filePointer))
     {
       character = fgetc(filePointer);
       printf("%c" , character);
     }


   fclose(filePointer);
   return 0;
}

I made a 'makefile' to compile the program and I made it so that the executable would be called 'prnt1'.

basically, like the following:

prnt1 : prnt.c
     gcc -ansi -Wall -pedantic prnt.c -o prnt1

The compilation worked, but whenever I run the program itself, it gives me a runtime error, saying: "Segmentation fault (core dumped)". When I look at the code itself, I don't seem to reach a memory that doesn't belong to me, so what could be an explanation for that problem and what can be done about it? Thank you in advance for your help.

Integrity
  • 23
  • 3
  • 2
    A good starting point would be to add the missing error checks that you should have. Check the return values from `malloc` and `fopen`, and make sure they aren't `NULL`. You really should do that as a bare minimum before asking for help. The most likely problem is that `fopen` failed, but since you don't check for it, you get a seg fault. – Tom Karzes Jun 10 '21 at 20:01
  • Also, don't use `feof`. Just check the return value from `fgetc` for `EOF`. – Tom Karzes Jun 10 '21 at 20:02
  • 1
    [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Nate Eldredge Jun 10 '21 at 20:52

1 Answers1

1

Since you said that the executable is named "prnt1" and the source file (which you want to read the code from) is named "prnt", argv[0] has the name of the executable (i.e. "prnt1") and, when ".c" is appended to argv[0], it becomes "prnt1.c" – which is definitely not the file you are trying to read from; athen, since this file doesn't exist, you're getting a segmentation fault.

So, as Tom Karzes said, always check the return value of fopen().

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83