Background: I’m using Mac/Unix and writing code in C on visual studio.
I’m attempting to copy an excel file from my computer’s directory into the character array “filedata,” using a relative file path. In order to accomplish this, I’m copying the current working directory (using the getcwd() function) into the Fileaddr character array, then appending the filename onto the end of the current directory string. Unfortunately, the fopen() function keeps failing to open as evidenced by the “failure” text that keeps printing as a result of fp being NULL. I’ve tested within both visual studio and the terminal directly.
In order to test the above, I’ve printed the string in my program being sent to fopen() via puts(concatenated value of string). From there, I’ve sent the command “open [filepath copied directly from visual studio printed text within the terminal]” and the file opens correctly in excel. When I type the above without the “open” command, I get a “permission denied” error (not sure this has something to do with the issue. In any event, my code is below. Any advice for figuring out what the issue is or how I can go about troubleshooting it?
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define MAXADDRSIZE 10000
#define MAXFILESIZE 1000000
int main()
{
char Fileaddr[MAXADDRSIZE];
getcwd(Fileaddr, MAXADDRSIZE);
char Filedata[MAXFILESIZE];
strncat(Fileaddr, "/MonthlyStateReport.xlsx\0", 30);
puts(Fileaddr);
FILE * fp=fopen(Fileaddr, "R");
if(fp==NULL)puts("failure");
fclose(fp);
}