-
January 22nd, 2004, 11:46 AM
#1
CFile sucks...
I Have only this small code:
CFile File;
if(File.Open("D:\\somedir\\log.txt",CFile::modeRead||CFile::modeCreate))
{
File.Close();
}
I do not see any mistakes, but he never enter the if case (He never closes the file). "D:\\somedir" does exists! Can somebody help me?
M. Donner
-
January 22nd, 2004, 11:54 AM
#2
Try looking here for the mistake:
Code:
CFile::modeRead||CFile::modeCreate
specifically the "||". You are doing a boolean or there not a bit-wise or. Try a single pipe character instead.
-
January 22nd, 2004, 11:57 AM
#3
Also, in future use of CFile::Open you can add the 3rd parameter, CFileException, wich will tell you the cause that a fail in CFile::Open().
-
January 22nd, 2004, 12:00 PM
#4
Oh, I am sorry, I have cpoied it wrong. In my code there is only one "|". It was show like this in the MSDN. If the File does not exists it has to be created. But neither he finds it nor he creates one.
M.Donner
-
January 22nd, 2004, 12:07 PM
#5
@ Doctor Luc: I Have tried...
FileException.Cause = 0;
in the ERRNO.H it means there is no error....
M.Donner
-
January 22nd, 2004, 12:45 PM
#6
Try calling GetErrorMessage from the Exception. If I had to guess, I'd say your directory doesn't exist.
-
January 22nd, 2004, 12:49 PM
#7
D: is a writeable drive isn't it? You're not trying to create this file on a CD ROM, for example?
-
January 22nd, 2004, 01:03 PM
#8
Another thought: Note that the combination of modeRead and modeCreate is quite unusual (if not invalid, then). After all, what's the point in creating a file for read access? The option to create a file makes sense when you can also write to that file. Or the other way round, a newly created file will always be empty, so there's nothing to read. I didn't test it, and the docs say nothing about that case, but it is possible that the combination of those two flags is even invalid, which would explain why you get an error.
-
January 22nd, 2004, 02:00 PM
#9
Re: CFile sucks...
I have just tested this code and it works fine. The only difference between code posted and verbatim copy is bitwise-OR ( | ) I used.
There are only 10 types of people in the world:
Those who understand binary and those who do not.
-
January 22nd, 2004, 02:06 PM
#10
OK, fine. So please ignore my last post (and look at what John E and GCDEF suggest instead).
-
January 22nd, 2004, 02:51 PM
#11
I used the following code and it worked for me; it gave an error when I use "X:" since that is not valid for my system. When the filename is valid, it opens the file but the file is always empty.
Code:
char Buffer[1000];
UINT Size;
CFileException Error;
CFile File;
if (!File.Open("X:Test.txt", CFile::modeRead|CFile::shareDenyNone|CFile::modeCreate, &Error)) {
Error.ReportError();
return nRetCode;
}
-
January 23rd, 2004, 11:02 AM
#12
@Sam Hobbes: IT WORKS! Thank you!
@gSterken: why create and read?
This file is an log file. when the application is run the first time, maybe this file does not exists yet. So the program has to create it. If this file already exists the app look for the of the file and saves the position. After this the file is closed. at another point of the programm (after writing in to this file) I can read only the newly wirtten data in it, because I know where the old data ends. So that is why I have used create an read mode at the same time.
@all: the fault:
I am working in a net of computers. And a coworker has already opend this file by mistake(he was looking for another log file), so I could not access it.
I did not found this mistake, because no message occured... now it is working fine. Thank you for all for your help.
M.Donner
-
January 23rd, 2004, 11:07 AM
#13
Originally posted by Maria Donner
I did not found this mistake, because no message occured... now it is working fine. Thank you for all for your help.
M.Donner
Did you call GetErrorMessage from the Exception or just look at m_Cause in the exceoption. A CFileException will report a sharing error.
-
January 23rd, 2004, 11:09 AM
#14
So, it wasn’t CFile that sucks.
There are only 10 types of people in the world:
Those who understand binary and those who do not.
-
January 23rd, 2004, 11:25 AM
#15
@ GCDEF:
m_Cause gave a "0" back... so I did not found a sharing error. I found the reason by accident...
@ JohnCz:
Yes you are right... It was only my prejudice, that first Microsoft is guilty, that sucks...
M.Donner
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|