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

  • Home
  • SEARCH
  • 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 8797151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:47:31+00:00 2026-06-13T23:47:31+00:00

I have the following program in C++: #include stdafx.h #include <fstream> #include <iostream> #include

  • 0

I have the following program in C++:

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <string.h>
#include <Windows.h>

using namespace std;

string integer_conversion(int num) //Method to convert an integer to a string
{
    ostringstream stream;
    stream << num;
    return stream.str();
}

void main()
{
    string path = "C:/Log_Files/";
    string file_name = "Temp_File_";
    string extension = ".txt";
    string full_path;
    string converted_integer;
    LPCWSTR converted_path;

    printf("----Creating Temporary Files----\n\n");
    printf("In this program, we are going to create five temporary files and store some text in them\n\n");

    for(int i = 1; i < 6; i++)
    {
        converted_integer = integer_conversion(i); //Converting the index to a string
        full_path = path + file_name + converted_integer + extension; //Concatenating the contents of four variables to create a temporary filename

        wstring temporary_string = wstring(full_path.begin(), full_path.end()); //Converting the contents of the variable 'full_path' from string to wstring
        converted_path = temporary_string.c_str(); //Converting the contents of the variable 'temporary_string' from wstring to LPCWSTR

        cout << "Creating file named: " << (file_name + converted_integer + extension) << "\n";
        CreateFile(converted_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL); //Creating a temporary file
        printf("File created successfully!\n\n");

        ofstream out(converted_path);

        if(!out)
        {
            printf("The file cannot be opened!\n\n");
        }
        else
        {
            out << "This is a temporary text file!"; //Writing to the file using file streams
            out.close();
        }
    }
    printf("Press enter to exit the program");
    getchar();
}

The temporary files are created. However, there are two main problems with this program:

1) The temporary files are not discarded once the application terminates.
2) The file stream is not opening the file and is not writing any text.

How can these problems be solved please? Thanks 🙂

  • 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-13T23:47:32+00:00Added an answer on June 13, 2026 at 11:47 pm

    When you supply FILE_ATTRIBUTE_TEMPORARY to Windows, is basically advisory — it’s telling the system that you intend to use this as a temporary file and delete it soon, so it should avoid writing data to disk if possible. It does not tell Windows to actually delete the file (at all). Perhaps you want FILE_FLAG_DELETE_ON_CLOSE?

    The problem with writing to the files appears pretty simple: you’ve specified 0 for the third parameter to CreateFile. This basically means no file sharing, so as long as that handle to the file is open, nothing else can open that file. Since you never explicitly close the handles you created with CreateFile, no other part of that program has an real possibility of writing to the file.

    My advice would be to pick one type of I/O to use, and stick to it. Right now, you have a combination of Windows-native CreateFile, C-style printf and C++ style ofstream. Frankly, it’s a mess.

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

Sidebar

Related Questions

If I have the following basic C++ program: #include <iostream> using namespace std; class
I have the following code: #include stdafx.h #include <iostream> #include <conio.h> using namespace std;
I have the following test program. #include <iostream> #include <cstdlib> using namespace std; pthread_mutex_t
I have the following code: //Comp454 program 2 #include <iostream> #include <string> #include <fstream>
I have the following C++ code: #pragma once #include StdAfx.h #include <string> using namespace
On Visual Studio 2010 the following program #include <iostream> using std::cout; int main() {
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return
I have the following program: #include <iostream> struct X { int a; float b;
i have following program to calculate size of file #include <iostream> #include <fstream> #include
I have the following program written in C: #include stdafx.h #include <Windows.h> void main()

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.