Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7941693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:44:42+00:00 2026-06-03T23:44:42+00:00

Whenever I create a Temporary file in C++ Native WinAPI, the HANDLE returned is

  • 0

Whenever I create a Temporary file in C++ Native WinAPI, the HANDLE returned is always INVALID_FILE_HANDLE but the temporary file has been created correctly?

Am I doing something wrong?

If I use GetLastError() the output is:

“The file exists”

In my code below I can successfully create a temporary file but the CreateFile function always returns INVALID_FILE_HANDLE and I can’t write any text to the file handle:

GetTempPath(dirLen, dir);
GetTempFileName(dir, fName, 0, tName);
HANDLE file = CreateFile(tName, GENERIC_WRITE, 0, NULL, CREATE_NEW,
                         FILE_ATTRIBUTE_TEMPORARY, NULL);
if (file == INVALID_HANDLE_VALUE) {
   outputLastError(); // Outputs: "The file exists"
}

if (FAILED(WriteFile(file, (LPTSTR)toWrite.c_str(), strLen, 0, NULL))) {
   cout << (_T("Failed to write string to file \r\n"));
   outputLastError();
   res = false;
}

// WriteFile doesn't fail but the temporary file is empty when I open it?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:44:43+00:00Added an answer on June 3, 2026 at 11:44 pm

    The GetTempFileName() will always create the file:

    If uUnique is zero, the function attempts to form a unique file name using the current system time. If the file already exists, the number is increased by one and the functions tests if this file already exists. This continues until a unique filename is found; the function creates a file by that name and closes it. Note that the function does not attempt to verify the uniqueness of the file name when uUnique is nonzero.

    CREATE_NEW is then specified in CreateFile() (as already pointed out by Mat) causing CreateFile() to return INVALID_FILE_HANDLE:

    CREATE_NEW
    Creates a new file, only if it does not already exist.
    If the specified file exists, the function fails and the last-error code is set to ERROR_FILE_EXISTS (80).
    If the specified file does not exist and is a valid path to a writable location, a new file is created.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whenever I edit files on emacs, it seems a temporary file is created with
I've been working on a system to keep a BRANCH_DESCRIPTION file whenever I create
I'm using ASP.Net and whenever I create a script file or try to include
Whenever I run my script a temp file gets created in my local directory:
This question has bothered me for a million years... whenever I create a website
Whenever I create a new Java file in Eclipse and check off the option
Whenever I create a new file, I would like to be able to include
Is there any method to create a file of desired size whenever I create
Whenever I create a new project in the Visual Studio 2010 Release Candidate, for
Whenever I create a Unordered List, A gap remains below each of the list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.