0

I wrote a simple makefile to run my code because I was asked to do so as a Deliverable I use fopen in my code,when I run the code from clion It locates the files I need normally, but when I run it from makefile it tells me that it wasn't able to find the files (my output always prints "Couldn't find ../a.txt")

-the function responsible to read (it runs normally and it could find the file if I ran it in clion) it exists in IO.c

void readPar(char *f, int *para) {
    FILE *file = fopen(f, "r");
    if (file == NULL) {
        printf("Couldn't find %s\n", f);
        exit(-1);
    }
    fscanf(file, "row=%d col=%d", &para[0], &para[1]);
    fclose(file);
}

-my main (called matMultp.c)

int main(int argN, char **argS) {
    char files[3][200] = {"../a.txt", "../b.txt", "../c.out"}; //default filenames

    if (argN > 1) { //if some argN was inserted
        for (int i = 1; i < argN; i++) {
            strcpy(files[i-1],"../");
            strcat(files[i-1],argS[i]);
            if(i==3)strcat(files[i-1],".out");
            else strcat(files[i-1],".txt");
        }
    }
    int para1[2] ; 
    readPar(files[0],para1);
    return 0;
}

-makefile

CC=gcc 
CFLAGS=-Wall -pthread -Wextra -g -Wstack-usage=1000 -fstack-usage
LDLIBS= -lpthread

all: matMultp
matMultp: matMultp.o matUtils.o IO.o
matMultp.o: matMultp.c 
matUtils.o : matUtils.c matUtils.h
IO.o : IO.c IO.h 
clean:
    rm -rf $(EXECUTABLE) $(OBJ)

if you want ls of my folder :

 a.txt  b.txt  cmake-build-debug  CMakeLists.txt  

dockDockBuildParams.json  IO.c  IO.h  IO.o  IO.su  

makefile  matMultp  matMultp.c  matMultp.o  matMultp.su  

matUtils.c  matUtils.h  matUtils.o  matUtils.su  tests
Omar Shawky
  • 1,242
  • 1
  • 12
  • 25
  • Maybe because the working directory used to executed differs? – MikeCAT Nov 15 '20 at 13:33
  • I don't understand what you mean – Omar Shawky Nov 15 '20 at 13:35
  • Where does your makefile *run* `matMultp`? Or have I misunderstood? – G.M. Nov 15 '20 at 13:38
  • they're all exist in the same folder,I just type make to build it then ./matMultp to run it,then it shows me the output, I edited the post to include the ls of my folder – Omar Shawky Nov 15 '20 at 13:41
  • You could see that all files exists in the same file both .o files,makefile and a.txt – Omar Shawky Nov 15 '20 at 13:42
  • If all of the files including the executable are in the same directory then you should probably have `"./a.txt"` (or just `"a.txt"`) rather than `"../a.txt"` etc. – G.M. Nov 15 '20 at 13:49
  • yes It ran normally now through makefile but clion now tells me it can't find a.txt – Omar Shawky Nov 15 '20 at 13:51
  • even with only a.txt clion can't find it – Omar Shawky Nov 15 '20 at 13:52
  • You can't reliably concatenate to a string literal with your `files[i - 1] = "../"; strcat(files[i-1], argS[i]);` and if that is still in the code then you *are* trying to look in a different folder anyway. – Weather Vane Nov 15 '20 at 13:59
  • @WeatherVane yes I know this for loop has something wrong regarding a segmentation fault caused from it if I entered parameters with (./matMultp) command,I'm trying to solve that now already, my problem in this question is just locating the files if i didn't enter parameters,it's either clion locates it but makefile doesn't or the opposite depending if I used ../a.txt or just ./a.txt – Omar Shawky Nov 15 '20 at 14:07
  • later I have to do the following (Your program is executed as: ./matMultp Mat1 Mat2 MatOut, where Mat1 and Mat2 are the names of the files to read the first and second matrixes, respectively. MatOut is the name of the file to write the output matrix. If the user does not enter this information, the default is a.txt and b.txt, for input matrixes A and B, respectively, and c.out for the output matrix) but that's later :D , only the default names matter now :\ – Omar Shawky Nov 15 '20 at 14:09
  • You could initialise a 2D array (of sufficient size) instead of an array of pointers. Each runtime argument you get, you copy it to the array. – Weather Vane Nov 15 '20 at 14:12
  • yea I solved and made an edit to the function thank you @WeatherVane – Omar Shawky Nov 15 '20 at 14:34
  • Good, but please don't change the question to the solution next time. If you want to, you can *add* the solution you found. – Weather Vane Nov 15 '20 at 14:37
  • you solved another problem I had&kept it for later but thank you anyway It would've taken me sometime to solve it if you didn't mention the solution, the addressed problem in this question has nothing to do with this segmentation fault, It still makes no difference to the problem with fopen, but now I understand what is the problem from Tomo's answer – Omar Shawky Nov 15 '20 at 14:41

1 Answers1

0

Try this and see if the answer dawns onto you:

void readPar(char *f, int *para) {
    FILE *file = fopen(f, "r");
    if (file == NULL) {
        printf("Couldn't find %s/%s\n", get_current_dir_name(), f);
        exit(-1);
    }
    fscanf(file, "row=%d col=%d", &para[0], &para[1]);
    fclose(file);
}

You will probably need to add #include <unistd.h> as well.

TCvD
  • 600
  • 2
  • 8
  • yes I understand now clion and makefile have different working directory and different executable places I will put makefile too in debug-build folder so they both have the same one,thank you – Omar Shawky Nov 15 '20 at 14:50
  • put it in another folder** so they don't conflict, there exist makefile and executable belonging to clion already in this folder :D – Omar Shawky Nov 15 '20 at 15:05