Tommy
Tommy

Reputation: 985

Can we pass a path string with white-spaces in it as argument to fopen()?

I'm using fopen() and I need to open a file where I pass a path with white-space in it as argument. Here is my code:

FILE * pFile;
pFile = fopen ("\this folder\myfile.txt","w");

Will that work as such or do I need to add something in there to recognize the space?Thanks.

Upvotes: 3

Views: 7981

Answers (4)

Paulo
Paulo

Reputation: 4333

Consider: rawurlencode()

If you are trying to load an external resource such as http://domain.com/path/to/file/filename has spaces.php then you might need to encode the url to avoid fopen() from failing.

In that case, you should actually call rawurlencode() since that will convert all necessary characters to %XY characters, including spaces. The regular urlencode() converts spaces to a + which won't help.

Caveat: I haven't tested this on an expansive list of character strings so it may create a different set of problems, but it worked for me in all cases I could find.

Upvotes: 0

ddd
ddd

Reputation: 9

not pFile = fopen ("\this folder\myfile.txt","w");

should be pFile = fopen ("\this folder\myfile.txt","wb");

Upvotes: 0

SatA
SatA

Reputation: 567

There is nothing special you need to do if the path has spaces in it.

pFile = fopen ("\\this folder\\myfile.txt","w");

Should work. Note the required double back-slashes in strings.

Upvotes: 5

Rohan
Rohan

Reputation: 53326

Spaces will work, but you need to escape '\' though, as

pFile = fopen ("\\this folder\\myfile.txt","w")

Upvotes: 5

Related Questions