fopen does not create new file for fwrite

This is a discussion on fopen does not create new file for fwrite within the C Programming forums, part of the General Programming Boards category; I'm trying to use fwrite to write an array of 65,536 8-bit values with a few other pieces of data ...

  1. #1
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582

    fopen does not create new file for fwrite

    I'm trying to use fwrite to write an array of 65,536 8-bit values with a few other pieces of data added in. While I know how to use fwrite, the problem I'm having is with fopen - the pointer is always 0 when using wb mode. Basically:

    Code:
    int StartHeight;
    char SlopeData[65536];
    
    ...
    
    // in a function
    GetCurrentDirectory(MAX_PATH, BasePath); // the location of the executable
    sprintf(FullPath, "%s\\data\\terrain\\Terrain03.dat", BasePath); // get the full path
    FileHandle = fopen(FullFileName, "wb"); // write output, binary mode
    
    if (FileHandle == 0) // if the file can't be found - report an error
    {
       sprintf(WarningString, "Error:  file cannot be opened for writing", FullFileName);
       MessageBox(WindowHandle, WarningString, "File cannot be opened", MB_OK | MB_ICONHAND);
    }
    
    else // file found - write the needed data
    {
       fwrite(&StartHeight, 4, 1, FileHandle); // the baseline
       fwrite(SlopeData, 1, 65536, FileHandle); // the main data
       fclose(FileHandle); // close the file releasing the handle
    }
    The content in the if statement is being used as the file handle is 0. That I'd expect. However, shouldn't fopen, for writing, create the file if it doesn't exist or do I have to go into Windows Explorer and manually create the files and folders as needed (which shouldn't be necessary)?
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It will create the file, if it can. fopen can't create a directory, for instance, so if there was no terrain subdirectory in your data directory already existing, then fopen will always fail (since it can't make that directory to put the file in).

    And I suppose it never hurts to print out your FullFileName just to make sure it's what you think it is.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    is FullFileName a valid filename? Do you have permission to create files in that directory? Does the file already exist and you don't have permissions to modify it?

    To answer your question, yes, fopen(filename, "wb") should create the file.

    It looks like you might be getting your FullPath and FullFileName variables mixed up? Hard to tell without seeing the rest of the code.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    tabstop pretty much explains it. Neither the "data" nor "terrain" directories exist. I thought fopen and the like actually created new directories as needed. Creating the directories in Windows Explorer has now made it work as expected. The data output is not quite what I'm expecting, upon viewing it in a hex editor (I've got a bug that lies elsewhere unrelated to this).
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ulillillia View Post
    I'm trying to use fwrite to write an array of 65,536 8-bit values with a few other pieces of data added in. While I know how to use fwrite, the problem I'm having is with fopen - the pointer is always 0 when using wb mode. Basically:

    Code:
    int StartHeight;
    char SlopeData[65536];
    
    ...
    
    // in a function
    GetCurrentDirectory(MAX_PATH, BasePath); // the location of the executable
    sprintf(FullPath, "%s\\data\\terrain\\Terrain03.dat", BasePath); // get the full path
    FileHandle = fopen(FullFileName, "wb"); // write output, binary mode
    
    if (FileHandle == 0) // if the file can't be found - report an error
    {
       sprintf(WarningString, "Error:  file cannot be opened for writing", FullFileName);
       MessageBox(WindowHandle, WarningString, "File cannot be opened", MB_OK | MB_ICONHAND);
    }
    
    else // file found - write the needed data
    {
       fwrite(&StartHeight, 4, 1, FileHandle); // the baseline
       fwrite(SlopeData, 1, 65536, FileHandle); // the main data
       fclose(FileHandle); // close the file releasing the handle
    }
    The content in the if statement is being used as the file handle is 0. That I'd expect. However, shouldn't fopen, for writing, create the file if it doesn't exist or do I have to go into Windows Explorer and manually create the files and folders as needed (which shouldn't be necessary)?
    Look closely... you string print the full path to FullPath but you're opening FullFilename

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >tabstop pretty much explains it. Neither the "data" nor "terrain" directories exist. I thought fopen and the like actually created new directories as needed. Creating the directories in Windows Explorer has now made it work as expected. The data output is not quite what I'm expecting, upon viewing it in a hex editor (I've got a bug that lies elsewhere unrelated to this).

    Have you read what itsme86 mentioned yet? He's got the solution for the problem mate. Looks through code!!

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    The problem is solved and I've already got the terrain loading as I'm expecting. Close the game engine and reopen it and I see the terrain exactly as I left it. The problem was that fopen does not create directories, it only creates files. When I posted my code, I changed names around. "FullPath" was actually originally "FullFileName", but I forgot to change this.

    The bug was that SlopeData in the engine is stored is as an array of ints when the file itself was intended to be written using unsigned chars. There are 75 unique slope steepness variations so, to efficiently store the data, using up the least amount of disk space possible, I intended to have the slope data stored as an 8-bit value - it was writing the 32-bit values one byte at a time. I just forgot to add in that conversion.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error fopen with existing file
    By cfdprogrammer in forum C Programming
    Replies: 8
    Last Post: 10-02-2009, 01:43 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21