Click to See Complete Forum and Search --> : Creating file: Path problem


torben00
October 22nd, 2004, 10:58 AM
hi,

When Iīm trying to create a file the path isnīt valid. From all examples I can find the use code like this:

String* path = "C:\\blabla.bmp";
FileStream* myFile = File::Create(path);
myFile->Wirte(......));

but I get the filename from another place so I have to Concat() them like this:

String* fileName = "blabla.bmp"
...
String* path = "C:\\";
FileStream* myFile = File::Create(String::Concat(path, fileName));

The first case works, but not this one. The problem is probably the double backslash ( \\ ), because if I write the "String::Concat(path, fileName)" to the screen the result is "C:\blabla.bmp

Does anyone have a solution to this problem???

cilu
October 22nd, 2004, 11:49 AM
\ is an escape char. Note that '\\' is a single char, not two. '\' this would give you a compile error, since the second ' would be take an a char and not delimiter. If you print '\\' or "\\" you get \. However, to have on screen \\ you must print "\\\\".

So your path string is ok.

Andreas Masur
October 22nd, 2004, 02:05 PM
[ Moved thread ]

Andreas Masur
October 22nd, 2004, 02:06 PM
Based on the given post, the backslashes cannot be a problem. Does 'Create' throw an exception? If yes, which one...if not, how do you know it is not working?

torben00
October 23rd, 2004, 05:47 PM
How I know it is that the file is not created. If I create the path manually the file is creted, but not with the concat. I hope I will solve it using the explanation earlier in this thread. Thanks guys!

Andreas Masur
October 24th, 2004, 12:30 AM
How I know it is that the file is not created. If I create the path manually the file is creted, but not with the concat. I hope I will solve it using the explanation earlier in this thread. Thanks guys!
Well...is any exception being thrown?

torben00
October 25th, 2004, 06:35 AM
Now Iīm back with the problem, I hope you still read ths thread.

Yes, it throws an Exception, looks like this:

System.ArgumentException: Illegal characters in path.

And I think it is because the path containes blanks (" "). Because I donīt know how big the filename is i use a byte[] with size 1024 when I recive it from the stream. I have tried to Trim() the String I get out from the byte[] but it dosenīt seams to work. So when I try to create the file with the path it has a size of 1027 (recieved filename + the gicen path).

Does anyone have any clue how to solve this? I canīt get the size of the stream in anyway so I know the size before I declare my byte[] that will read the stream? Or is my server so bad coded so I have to do this in a totaly differen way?

Help!!! Please!!!

Andreas Masur
October 25th, 2004, 01:39 PM
Can you actually post the code part where you get the filename?

torben00
October 26th, 2004, 09:20 AM
Here I get the filename, just the name, for example myfile.exe, and sends it to the server.

String* path = openFileDialog1->FileName;
FileStream* myFile= File::Open(path, FileMode::Open);
FileInfo* fi = new FileInfo(path);
String* myFileLength = myFile->Length.ToString();
String* myFileName = fi->Name;

Then I send the filename to the server so he can create the file to write to.

Then I recieve the name and put in a String the length of it is much bigger than the actual size but looks correct when I print it to the screen, and I have tried all kind of methods to get all possible blanks out from the string. Say that the size is 1024 and the actual string just is 10. What is the rest of the files?

And here is the code on the server side:


Byte byteName[] = new Byte[1024];
Int32 nrOfBytes = stream->Read(byteName, 0, byteName->Length);
String* fileName = System::Text::Encoding::ASCII->GetString(byteName);
String* path = "C:\\\\";
String* myEndPath = String::Concat(path, fileName);

Musti
April 16th, 2006, 01:40 PM
Hi,
I have the same problme like torben00 , can somean help me ?because i don't see the answer to torben00 problem posted here.

cilu
April 17th, 2006, 04:16 PM
Hi,
I have the same problme like torben00 , can somean help me ?because i don't see the answer to torben00 problem posted here.
I'm not sure I understand the problem, but try this:

Byte byteName[] = new Byte[1024];
Int32 nrOfBytes = stream->Read(byteName, 0, byteName->Length);
String* fileName = System::Text::Encoding::ASCIIEncoding->GetString(byteName, 0, nrOfBytes);

Notice that this is wrong:

String* path = "C:\\\\";

It should be:

String* path = "C:\\";

I don't understand why the string read from the stream is concatenated to C:\, but, well, that's not my problem. ;)