CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: CFile sucks...

  1. #1
    Join Date
    Oct 2003
    Posts
    58

    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

  2. #2
    Join Date
    Nov 2003
    Posts
    74
    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.

  3. #3
    Join Date
    Nov 2001
    Location
    Beyond Juslibol
    Posts
    1,688
    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().

  4. #4
    Join Date
    Oct 2003
    Posts
    58
    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

  5. #5
    Join Date
    Oct 2003
    Posts
    58
    @ Doctor Luc: I Have tried...

    FileException.Cause = 0;
    in the ERRNO.H it means there is no error....

    M.Donner

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635
    Try calling GetErrorMessage from the Exception. If I had to guess, I'd say your directory doesn't exist.

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835
    D: is a writeable drive isn't it? You're not trying to create this file on a CD ROM, for example?

  8. #8
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    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.

  9. #9
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    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.

  10. #10
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    OK, fine. So please ignore my last post (and look at what John E and GCDEF suggest instead).

  11. #11
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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;
    	}
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  12. #12
    Join Date
    Oct 2003
    Posts
    58
    @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

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635
    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.

  14. #14
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917
    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.

  15. #15
    Join Date
    Oct 2003
    Posts
    58
    @ 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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured