Hi people, need some help in closing the window after program execution has completed.
I am running Turbo c++ 4.5 for windows under Win7 32. Thanks.
Heres the sample code:

//Read and display test customer file

#include <stdio.h>
#include <conio.h>
#include <iostream.h>

struct customer
{
 char c_name[50];
 int i_age;
};

int main(void)
{
 clrscr();
 struct customer rec;
 FILE *stream;
 if((stream = fopen("Personel.dat", "r" )) == NULL)
  {
    cout << "Error opening file\n";
    return 1;
  }
 while(!feof(stream))
  {
    fread(&rec, sizeof(rec), 1, stream);
     {
      if(feof(stream)) break;
     }
    cout << rec.c_name << "   age " << rec.i_age << "\n";
  };

 fclose(stream);
 cout << "\n\nEnd of file!";
 getch();
 return 0;
}

Dani AI

Generated

What you are seeing is normal console-host behavior on Windows. Your program does not own that window; the console host (conhost/NTVDM, or the IDE when you run from inside it) does. When a console app exits, Windows closes the console only if your process was the last one attached to it. If you launch from an existing console (cmd.exe) or from Turbo’s IDE, that window stays open because it is not your process’s window. That is why ’s advice is correct: there is nothing portable you can add to the program to force-close a window you do not own.

Avoid trying to call GUI-only APIs like DestroyWindow as suggested by ; console windows are not ordinary HWNDs you created, and that call is unreliable here. For Win32 targets, the proper API is FreeConsole(), which detaches your process from its console; the window closes only if you were the last attached process, which is the safe behavior when double-clicking an .exe. See Microsoft’s docs: FreeConsole. Note that Turbo C++ 4.5 for Windows is from the Win16 era, so Win32 console APIs may not be available; in that case your practical options are to run the .exe outside the IDE or move to a modern compiler as hinted.

Unrelated to closing the window, your file-reading loop will be more robust if you drive it by the success of fread instead of feof, which is only set after a failed read:

while (fread(&rec, sizeof rec, 1, stream) == 1) {
    cout << rec.c_name << "   age " << rec.i_age << '\n';
}
if (ferror(stream)) {
    perror("read error");
}

Recommended Answers

All 7 Replies

Well, the line "getch();" is going to wait for the user to type a character before exiting the program. Remove it and the program will exit after printing "End of file!" and the console window will close.

Thanks Mike_2000_17 for your response. I removed the getch() line but the window still wont close. Anything else I can do?

If it does print "End of file", then what you need to do to close the window is to click on the X in the upper right corner. Once it reaches the end of the program, if the window doesn't close by then, then there's nothing to put in your program that will close the window.

I am not using that sample code for anything but pasted it for reference to my question. Yes it prints the files contents and the "End of file" but hangs there until I physically close the window. There has to be something that will close the console window surely?.

Perhaps it has something to do with the fact that you're using a freaking ancient compiler with a state of the art OS. Do you have a similar problem with other free compilers that were written for something newer than Windows 3.1?

You have a point there. I'll go dig up my old XT.

#include <windows.h>
int main()
{
  //whatever
  DestroyWindow(GetActiveWindow());
  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.