Howler72

Hello All,

I am having a hard time trying to figure out why my code does not work. I was tasked with writing a small win 32 console app to accept user directory and file name to write to the local machine. My app creates the directory at this point however I cant the file to be written. What am I doing wrong All help appreciated.

Code Snippet

int main(){

char userDirectory [40]= " ";

char fileName [40]= " ";

char dirAndFile [100]=" ";

cout<<"Enter the fully qualified path of the directory you would like to make: ";

cin>>userDirectory;

cout<<"\nEnter the name of file to create in "<<userDirectory;

cin>>fileName;

CreateDirectory(userDirectory, NULL);

strcat_s(dirAndFile,userDirectory);

cout<<dirAndFile;

strcat_s(dirAndFile,fileName);

cout<<dirAndFile;

HANDLE hFile;

hFile= CreateFile(dirAndFile, GENERIC_WRITE, FILE_SHARE_WRITE,

NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

return 0;



Re: Visual C++ General File Handler not working

Aleksandr Tokarev

Remove spaces in userDirectory, fileName, and dirAndFile varibles initialization. And add CloseHandle after the file created.

char userDirectory [MAX_PATH]= "";

char fileName [MAX_PATH]= "";

char dirAndFile [MAX_PATH]="";

cout<<"Enter the fully qualified path of the directory you would like to make: ";

cin>>userDirectory;

cout<<"\nEnter the name of file to create in "<<userDirectory;

cin>>fileName;

CreateDirectoryA(userDirectory, NULL);

strcat_s(dirAndFile,userDirectory);

cout<<dirAndFile;

strcat_s(dirAndFile,fileName);

cout<<dirAndFile;

HANDLE hFile;

hFile= CreateFileA(dirAndFile, GENERIC_WRITE, FILE_SHARE_WRITE, // Are you sure that you need share write

NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

if(hFile != INVALID_HANDLE_VALUE)

{

// Write file here.

CloseHandle(hFile);

}





Re: Visual C++ General File Handler not working

Simple Samples

Can you be specific about what does not work or that happens that is not supposed to happen

Also, note that CreateDirectory returns BOOL and CreateFile returns INVALID_HANDLE_VALUE if the function fails; please do check return codes in your program.

Also, you are very likely to be able to solve the problem if you use the debugger to single-step through the program.






Re: Visual C++ General File Handler not working

Howler72

Sure enough the CreateFile returns an INVALID_HANDLE_VALUE. The build succeeds, so I am really curious as to why my program creates a directory, but will not write a file in that directory. I will try to another route instead of using the windows.h, I will try using the direct.h with the _mkdir method.

O





Re: Visual C++ General File Handler not working

Aleksandr Tokarev

Howler72,

Read my post above, you need to remove spaces in directory and file name initialization varibles.

Your code,

char userDirectory [40]= " ";//<- remove this breakspace.

char fileName [40]= " ";//<- remove this breakspace.

char dirAndFile [100]=" ";//<- remove this breakspace.

My code,

char userDirectory [MAX_PATH]= ""; // <- empty string, without a breakspace.

char fileName [MAX_PATH]= "";// <- empty string, without a breakspace.

char dirAndFile [MAX_PATH]="";// <- empty string, without a breakspace.

And everything will work greatly. Or almost greatly if you close handle, and check the returning values. :-)




Re: Visual C++ General File Handler not working

Simple Samples

It is good that you have determined that CreateFile is returning an error. It is not good that there is an error but since the program was not working it is good that you have isolated the problem.

Are you also calling GetLastError

You will probably determine the problem using the debugger. Be methodical; use the debugger to see what is happening. Don't guess; using the _mkdir method is unlikely to fix the problem.

There is probably a problem with backslashes. Perhaps there is not a backslash between the directory and the filename. That is why I say to use the debugger.

I doubt that the spaces in the strings to initialize them is the problem. I assume they will be overwritten by the new data read in.






Re: Visual C++ General File Handler not working

Howler72

The error appears to be related to the fact that I am using strcat_s with. Apparently when I try to the aforementioned function, the display of the variable looks good with a cout. However internally, the structure causes a problem with the createfile method, as well as the ostream method. I will carry on.






Re: Visual C++ General File Handler not working

Aleksandr Tokarev

Please, compile my code it works.





Re: Visual C++ General File Handler not working

Howler72

I just read your reply Aleksandr, by removing the spaces worked. What was I doing wrong A big thank you!!

O