#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    7
    Rep Power
    0

    fopen returns null on Win 98


    Hi,

    This is such basic code and yet I can't figure out the problem. Here's the code - explanation is beneath.

    int readSettingsFile(char *filePath, char *fileContents)
    {
    FILE *pFile;

    // some non relevant code here ...

    // open file for reading
    if((pFile=fopen(filePath,"rt"))==NULL)
    {
    strcpy(fileContents, CANNOTREAD);
    return FALSE;
    }

    // more non relevant code ...

    fclose(pFile);
    }

    The code in this function should open a file - "C:\Temp\settings.xml" - for reading (the file does exist). I pass filePath using double slashes ie "C:\\Temp\\settings.xml" and everything works fine on Windows XP/2000. However as soon as I try it on Windows 98 the fopen returns NULL. Built using MSVC++ 6.0. Have tried building on both platforms to see if that helped but it doesn't.

    I'd be grateful to hear from anybody who knows why this might be happening.

    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    55
    Rep Power
    12
    Have you even checked what errno has to say about it?
    If the file does not exist and you want to create it, you should pass 'w' inside your mode string ("w+t").
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    7
    Rep Power
    0
    Oops - sorry.

    errno returns ENOENT (2) file or directory don't exist. That's the infuriating thing - they do exist.

    I've even tried writing the path directly to the fopen call ...

    if((pFile=fopen("C:\\Temp\\settings.xml","rt"))==NULL)

    but I get the same problem. What could be wrong?

    I don't want to destroy the contents of the existing file so haven't used "w+t". However just to see what happened I tried it but it couldn't even create the file - still returned ENOENT.

    Any ideas?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    55
    Rep Power
    12
    So use "a+t", please read the documentation.
    Do you even have permissions to write to the directory?
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    7
    Rep Power
    0
    Yes I have OK permissions. Am freely able to create files at this location. It's a problem on more than one Windows 98 pc so didn't think permissions to be an issue.

IMN logo majestic logo threadwatch logo seochat tools logo