Results 1 to 3 of 3

Thread: [RESOLVED] How can I check a file open/write error on Windows 9x while using fstream library?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    12

    Resolved [RESOLVED] How can I check a file open/write error on Windows 9x while using fstream library?

    Hi. I am using std::ofstream to write a log file. I know I can check if there was an error during opening or writing to the file by checking for failbit or badbit, but that doesn't give me any information to what actually happened, only that something happened. How can I check and detect what actually happened when opening the file or writing to it? I am using Visual C++ 6.0 under Windows 98 SE, a guest OS on VMware Workstation 15 (non-commercial/non-profit licence) hosted by my Windows 10 64bit machine.

    I even prepared my own error codes in errcodes.h file:
    Code:
    /***********************************
     * Kris-Kros Game Kit
     *----------------------------------
     * Kris-Kros Map Generator source
     * File: errcodes.h
     *
     * Made by Marek Poláček
     * Github.com/Polda18
     *
     * GNU-GPL v3.0
     * 2019
     ***********************************/
    
    #ifndef ERRCODES_H
    #define ERRCODES_H
    
    #define ERR_ARG_INVALID               1
    #define ERR_FILE_FORMAT_INCORRECT     2
    #define ERR_FILE_NOT_FOUND            3
    #define ERR_FILE_WRITE_PROTECTED      4
    #define ERR_FILE_DAMAGED              5
    #define ERR_FOLDER_NOT_FOUND          6
    #define ERR_FOLDER_WRITE_PROTECTED    7
    #define ERR_FOLDER_DAMAGED            8
    #define ERR_INSUFFICIENT_PERMISSION   9
    // Next error codes based on requests
    
    #define ERR_UNEXPECTED               99
    // Unexpected error, return code may be raised if reached in future (highly unlikely)
    
    #endif

    And a function to write a log file, now yet without the file write function to it, in logger.h and logger.cpp files:
    Code:
    /***********************************
     * Kris-Kros Game Kit
     *----------------------------------
     * Kris-Kros Map Generator source
     * File: logger.cpp
     *
     * Made by Marek Poláček
     * Github.com/Polda18
     *
     * GNU-GPL v3.0
     * 2019
     ***********************************/
    
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <ctime>
    #include <errno.h>
    
    #include "errcodes.h"
    #include "commands.h"
    #include "logger.h"
    
    int mapgen::logger::log(std::string lFname, std::string lMsg)
    {
        int rCode = 0;
    
        std::string lFullFname = lFname + ".log";
    
        time_t raw;
        time(&raw);
    
        struct tm *tInfo;
        char buff[80];
    
        timeinfo = localtime(&raw);
        strftime(buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", tInfo);
    
        std::string now(buff);
        // [YYYY-MM-DD HH:MM:SS] <lMsg>
    
        // TODO: write into log file
    
        return rCode;
    }

    My question is: how can I get the exact reason of failure if it happens? I want it to work under Windows 9x, simply because I want it to work under pretty much entire Windows versions span on 32bit architecture. Applications built under and for Windows 9x work under all versions of Windows, even on 64bit Windows 10, latest build. This program I am building (Kris-Kros Map Generator - mapgen.exe) is part of my game development, Kris-Kros. It's all in C++

    Entire source code can be found on GitHub:
    https://github.com/Polda18/Windows-Kris-Kros-Game

  2. #2
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    991

    Re: How can I check a file open/write error on Windows 9x while using fstream library

    Under Windows, if the i/o functions fail, then the global variable errno contains the error number.

    errno values are documented at https://docs.microsoft.com/en-us/cpp...r?view=vs-2019
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    12

    Re: How can I check a file open/write error on Windows 9x while using fstream library

    Thank you

Tags for this Thread

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