Click here to Skip to main content
15,879,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For example, I want to write file "a.txt" into D:\program, and I did
FILE* f=fopen("D:\program\a.txt","wb").
I only want the path of file to be D:\program, but
FILE* f=fopen("D:\program","wb") is wrong.
Posted
Updated 28-Dec-10 20:25pm
v2
Comments
yaprig 29-Dec-10 2:24am    
what error did u see?
Rajesh R Subramanian 29-Dec-10 3:00am    
Your query isn't clear.

Difficult to be precise, but there are two things that pop to mind:
1) You are trying to create a file called "program" in the root of the D: disk which contains a folder called "program" allready.
2) You are trying to create a file called "program" in the root of the D: disk which contains no such file or folder.

If the first case:

Yes, that is wrong.
What do you expect to happen?
Look at it this way:
When you file a letter in a file cabinet, do you try to write all letters on the file divider? Or on individual sheets of paper that you insert into the file divider?

You must specify the file name when you use fopen or you are trying to write to the folder, which you are not allowed to do.

If the second case:

Do you have permission to write to the root directory of the D: drive?
 
Share this answer
 
Comments
Rajesh R Subramanian 29-Dec-10 2:59am    
I wonder why was that down-voted. No comments by the voter either.
Didn't understand your question, anyway if you are trying to create a diretory use function CreateDirectory , like CreateDirectory("C:\\test",NULL);
 
Share this answer
 
I want to create output directory and save "a.txt".
I'm not sure about <windows.h>
Can you help me?
 
Share this answer
 
Yes.

i think you are not familiar with windows programming. windows.h is one of the basic header we need.

this link contains some tutorials on windows programming. http://www.winprog.org/tutorial/start.html[^]

you can crate a directory by calling the function CreateDirectory.

later you can write the file to that directoryusing fopen(filename,"w") and using fwrite or fprintf or whatever.
 
Share this answer
 
You're not escaping the backslash correctly. If you want to open a file in C:\some_dir you have to use:

"C:\\some_dir\\a.txt"

as the directory name otherwise the compiler thinks there're escaped 's' and 'a' characters in the string.

Cheers,

Ash

PS: As you're using C++ don't mess about with fopen and the like. Use the stream classes unless you have to be backward compatible with C.

PPS: On Windows you can't open directories the way you can under old versions of UNIX.
 
Share this answer
 
v2
For more details:
I am implementing command arguments
<File_Name> <Output_Directory>
EX:
<File_Name> : D:\a.txt
<Output_Directory> : D:\Compressed
but finally
Output: D:\Compressed\a.huf
 
Share this answer
 
Dear Adamjohnvt,

It is not the way to create a file in folder or write in a file. In this regard, Please read the following:
http://msdn.microsoft.com/en-us/library/as2f1fez(v=VS.90).aspx[^]

Regards!
Aman
 
Share this answer
 
Comments
OriginalGriff 29-Dec-10 3:09am    
You well be wrong: Note that the OP is using C++: this could well be native code, in which case .NET classes and procedures do not apply. For native code, fopen is perfectly valid.
Aman4.net 29-Dec-10 4:01am    
Point is noted :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900