Open file with fopen, given absolute path on Windows
Use double slashes:
"C:\\Documents and Settings\\juegos psps.txt"
An absolute path in fopen()
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
C fopen() - Possible issue with absolute path
You're returning the address of a local variable in getFileName
, which results in undefined behavior.
This is a common pitfall in C.
You need to either:
A) Allocate the string on the heap (using e.g. malloc
) and return it.
B) Have getFileName
take a pointer to a caller-allocated buffer which it then populates.
Also, when debugging problems like this, don't just assume everything is working. Use printf
to see what the value of filename
is before you try to fopen
it.
C++ fopen relative Path
Relative paths are relative to the current working directory, not the path of the executable. The current working directory is the directory from which you started the program.
To treat a path as relative to the position of the executable, the simplest portable option is to access the executable as argv[0]
, extract the directory, and chdir()
into it. Note that this will work only as long as the program was itself started with the full path name.
C fopen can't open absolute path OS X
file://localhost/Users/macuser/test.txt
is a URI, not a file path. Process the string to strip out everything up to and including /localhost
and it should be fine.
Note that this sort of solution will only work if there are no escape sequences in the URI. As noted below, it may be simpler to send the path from the Objective C side.
Unable to access file from share drive using fopen in C (windows)
You need to escape the backslash in filepath during compile time.. i.e.
"\xyz\abc.txt" but runtime has only one slash.
Piece of code to read input from user:
char filename[50];
FILE *fp;
printf("Enter the filename \n");
gets(filename);
fp = fopen(filename, "r");
Related Topics
How to Enforce the 'Override' Keyword
Why Can't We Declare Object of a Class Inside the Same Class
Does Clearing a Vector Affect Its Capacity
Multiple Inheritance: Unexpected Result After Cast from Void * to 2Nd Base Class
How to Initialize the Reference Member Variable of a Class
Calling Base Class Definition of Virtual Member Function with Function Pointer
Qt 5 and Qprocess Redirect Stdout with Signal/Slot Readyread
Convert Inline Assembly Code to C++
Shared_Ptr and Weak_Ptr Differences
Adding Signals/Slots (Qobject) to Qgraphicsitem: Performance Hit
Free Function Versus Member Function
How to Load & Call a Vbscript Function from Within C++
Effective Use of C++ Iomanip Library
Brace Initialization for Inherited Pod