Click here to Skip to main content
15,905,233 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am creating/opening a file using,
C++
FILE *file = fopen(" C:\\ABC\\Poo.txt","w+");
if(file == NULL)
printf("un sucessful");
else
printf("sucessful");


I am able to create a file name poo.txt on C:\ABC folder.

Now I used,
C++
#define PATH "C:\\ABC\\"
FILE *file = fopen(" PATH+Poo.txt","w+"); 

OR
C++
#define PATH "C:\\ABC"
FILE *file = fopen(" PATH\\Poo.txt","w+"); 
if(file == NULL)
........


File is not get created without any error.
How can I use PATH constant with file name in fopen?
Please help me out.
Thanks in advance.
Posted
Updated 8-Apr-10 21:39pm
v4

Try it :) :
#define MAX_PATH 256
#define ROOT_DIR "C:\\ABC\\"

// 1)
{
  char szPathedFileName[MAX_PATH] = {0};
  sprintf(szPathedFileName, "%s%s", ROOT_DIR, "test.txt");
  // use the buffer now
...
}

// 2)
{
  char szPathedFileName[MAX_PATH] = {0};
  strcat(szPathedFileName, ROOT_DIR);
  strcat(szPathedFileName, "test.txt");
  // use the buffer now
...
}

When you will get any compiler errors here -
please see the secure replcement with: strcpy_s and sprintf_s :)
 
Share this answer
 
Poonamol wrote:
FILE *file = fopen(" PATH+Poo.txt","w+");


Change to
C++
FILE *file = fopen(PATH "Poo.txt", "w+"); 

:)
 
Share this answer
 
Nice One.
This is simple and workable solution.

But if my filename is stored in one variable then how I'll use fopen.
Using Sprintf/strcat only?

Thanks a lot.
 
Share this answer
 
v2

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