2
votes

I am trying to make my c program more dynamic. It should opens a file with fopen(). Apparently, fopen does not read absolute paths. For example It can't read this path:

fopen("/Documents/projects/cs50_radio/broadcast/source/deadinside.mp3", "r")

returns NULL

;however,

fopen("deadinside.mp3", "r");

returns the expected pointer

I was wondering if there is a possible way to read such a path which might be independent from the current working directory in other cases ?

1
Did you mean to say Relative Path? - glant
fopen() can definitely open files using full path spec. Possibly a typo? - JJF
Is this on Windows? - kaylum
Try fopen("\\..\\..\\..\\..\\..\\"deadinside.mp3" ,"r") or read this link:stackoverflow.com/questions/8819800/… - Omid CompSCI
I reviewed my code again and I found out I was not terminating my string which caused this error. thank you all for the replies and sorry for the inconvenience - A.Emad

1 Answers

4
votes

fopen() can take absolute paths as arguments. Are you working on a unix/linux based OS or on windows? Likely what is happening is you've got the path wrong. If you're on a mac, which it looks like you are, the correct path might be

~/Documents/projects/cs50_radio/broadcast/source/deadinside.mp3

But you can verify by cd'ing into the directory and typing pwd

If you're on windows, your path is definitely wrong, as windows would look more like this:

C:\Documents\projects\cs50_radio\broadcast\source\deadinside.mp3