open lots of text

Working with microsoft visual studio. Wondering if there is a way to get a program to open the exact same .txt file many times and then close the program. Thanks.
(I only know very very basic c++ [cout, cin, if else, really only that] so I may be easily confused haha.)
EDIT: I also want both the TXT files and the program to shut down (i do not know if they shut down at the same time)
Last edited on
Use a loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <fstream>
#include <iostream>

int main()
{
    for (int i = 0; i < 100; i++)
    {
        std::ifstream file("file.txt");
        if (!file)
        {
            std::cout << "Error: Could not open file.txt\n";
        }
    }
}


It's not clear what you're actually trying to accomplish.
Last edited on
I want the program to open a bunch of windows of that txt file, and then shutting down the program and possibly the txt files if that is possible (Shutting down both the txt and the EXE itself)
you can certainly do this.
you don't need to open the file more than once, you can read it into a buffer and throw it onto each window you spawned, say into a multi-line edit control? But you need to create each window... which you may need to learn how to do.
you can also open like 5000 copies of notepad, if you want. Closing them is possible, but you need to get some system level info (process ID, probably) to do that.

What do you REALLY want to accomplish? You can do this, but it seems... useless ...
Last edited on
Still an odd request, I feel like we're still missing a piece of this puzzle.

The program itself ending is easy; you just return from main.

The harder part is keeping track of the all the [default txt file program] instances you have open. If you're on Windows, you could use Win32 to call CreateProcess in a loop, keep track of all the process ids created, and then forcibly kill all those processes at the end of the program.

https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa

It's not super hard, but also not exactly trivial for a beginner since the Win32 API isn't exactly beginner friendly. If I have time I could try to hack something up later, but I'm less inclined to do so because I don't see the usefulness of this (so maybe it would help explain why this is a useful thing to do :).
Last edited on
Sounds to me like the OP wants to create one of those joke programs that does something to an unsuspecting user, such as open a bunch of windows all reading





                    I love you                    




or something, then cleaning itself up because the joke would be mean otherwise.

This can be done, and fairly easily too, but it isn't simple or straight-forward. You might want to try using an easier technology than straight C/C++. For example, if you have Tcl/Tk installed you can run this script.

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

proc Pack.Window w {
  pack [label $w.text -text "I love you."] -expand yes -fill both
  set width  400
  set height 300
  set x [expr {int(rand() * ([winfo screenwidth  .] - $width))}]
  set y [expr {int(rand() * ([winfo screenheight .] - $height))}]
  wm geometry $w ${width}x$height+$x+$y
  wm title $w :O)
}

proc Create.Window {} {
  set w .w[incr ::N]
  Pack.Window [toplevel $w -bg #FFF]
  wm protocol $w WM_DELETE_WINDOW {destroy .}
}

Pack.Window .
for {set n 0} {$n < 10} {incr n} Create.Window
after 150 {focus -force .}

Produces 10 windows, all identical, and closes them all when any one of them is closed.
(You can get Tcl/Tk at https://www.activestate.com/products/tcl/downloads/. Get the 8.6 version.)

Enjoy!
Last edited on
That's true, I suppose there isn't much harm in it.
Exactly what Duthomhas (12529) said, except in c++ because that is all i can/want to use.
Ok, here's the same for Windows API.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <chrono>
#include <random>
#include <windows.h>

#pragma comment(lib, "user32")
#pragma comment(lib, "gdi32")

#pragma comment(linker, "\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")


int random( int range )  // Return a random number in [0, range-1]
{
  static std::ranlux48 prng( std::chrono::system_clock::now().time_since_epoch().count() );
  std::uniform_int_distribution <unsigned> dist( 0, range - 1 );
  return dist( prng );
}


LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
{
  constexpr WORD IDC_STATIC_TEXT = 101;

  switch (msg)
  {
    case WM_CREATE:
    {
      RECT rect;
      GetClientRect( hwnd, &rect );
      HWND htext = CreateWindowExA(
        0,
        "Static",
        "I love you",
        WS_CHILD | WS_VISIBLE | SS_CENTER | SS_CENTERIMAGE,
        0, 0, rect.right, rect.bottom,
        hwnd,
        (HMENU)IDC_STATIC_TEXT,
        NULL,
        NULL
      );
      HFONT hfont = (HFONT)GetStockObject( DEFAULT_GUI_FONT );
      SendMessage( htext, WM_SETFONT, (WPARAM)hfont, MAKELPARAM( FALSE, 0 ) );
      break;
    }

    case WM_SIZE:
    {
      RECT rect;
      GetClientRect( hwnd, &rect );
      HWND htext = GetDlgItem( hwnd, IDC_STATIC_TEXT );
      SetWindowPos( htext, NULL, 0, 0, rect.right, rect.bottom, SWP_NOZORDER );
      InvalidateRect( hwnd, NULL, FALSE );
      break;
    }

    case WM_DESTROY:
    {
      PostQuitMessage( 0 );
      break;
    }

    default:
      return DefWindowProc( hwnd, msg, wparam, lparam );
  }

  return 0;
}


int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE, LPSTR, int nCmdShow )
{
  const char* szWindowClassName = "ILU";
  const char* szWindowTitleText = ":O)";

  WNDCLASSEXA wc;
  wc.cbSize        = sizeof(WNDCLASSEX);
  wc.style         = 0;
  wc.lpfnWndProc   = &WndProc;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.hInstance     = hinstance;
  wc.hIcon         = (HICON)  LoadImageA( NULL, IDI_APPLICATION, IMAGE_ICON,   0, 0, LR_DEFAULTCOLOR | LR_SHARED | LR_DEFAULTSIZE );
  wc.hCursor       = (HCURSOR)LoadImageA( NULL, IDC_ARROW,       IMAGE_CURSOR, 0, 0, LR_DEFAULTCOLOR | LR_SHARED | LR_DEFAULTSIZE );
  wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  wc.lpszMenuName  = NULL;
  wc.lpszClassName = szWindowClassName;
  wc.hIconSm       = (HICON)  LoadImageA( NULL, IDI_APPLICATION, IMAGE_ICON,   0, 0, LR_DEFAULTCOLOR | LR_SHARED | LR_DEFAULTSIZE );

  if (!RegisterClassExA( &wc ))
  {
    MessageBoxA( NULL, "Fatal error: Window Registration Failed!", szWindowClassName, MB_ICONEXCLAMATION | MB_OK );
    return 1;
  }

  UINT style    = WS_OVERLAPPEDWINDOW;
  UINT ex_style = WS_EX_CLIENTEDGE;

  RECT rect { 0, 0, 400, 300 };
  AdjustWindowRectEx( &rect, style, 0, ex_style );

  RECT workarea;
  SystemParametersInfo( SPI_GETWORKAREA, 0, &workarea, 0 );


  for (int n = 0; n < 10; n++)
  {
    HWND hwnd = CreateWindowExA(
      ex_style,
      szWindowClassName,
      szWindowTitleText,
      style,
      workarea.left + random( workarea.right - workarea.left - rect.right + rect.left ), // x
      workarea.top  + random( workarea.bottom - workarea.top - rect.bottom + rect.top ), // y
      rect.right - rect.left, // width
      rect.bottom - rect.top, // height
      NULL,
      NULL,
      hinstance,
      NULL
    );
    if (!hwnd)
    {
      MessageBoxA( NULL, "Fatal error: Window Creation Failed!", szWindowClassName, MB_ICONEXCLAMATION | MB_OK );
      return 2;
    }

    ShowWindow( hwnd, nCmdShow );
    UpdateWindow( hwnd );
  }


  MSG msg;
  while (GetMessage( &msg, NULL, 0, 0 ) > 0)
  {
    TranslateMessage( &msg );
    DispatchMessage( &msg );
  }
  return (int)msg.wParam;
}

Compile with:

cl /EHsc /Ox /W4 ilu.cpp
mt -manifest ilu.exe.manifest -outputresource:ilu.exe
del *.obj *.manifest

You will have to start reading up online on how to use the Windows API if you want to use a text edit control. Loading text from a file is the same easy-peasy C++ stuff as always.

Good luck!
Well I figured I'd have some fun and wrote it again using X11.
Then when I ran it I remembered that X11 can't do decent text. Never could.

So I figured I might as well play around with gtkmm, something I've been meaning to learn for a while now. Gtk+ seems nice. Gtkmm is a nightmare. Sure, still very convenient C++ and small code (86 LOC to do a few extra things besides just what I have previously done, compared to the 139 LOC for the X11 version). But figuring out how to do simple things was a deep trek through SO and random other sites alone, since apparently gtkmm changes its API constantly.

I suspect a purely Gtk+ version would be even smaller. The text renders beautifully, though... (Thanks freetype2)


If anyone is the least bit curious I'll post the code for them. If not I won't bother.

Heh...



Maybe I'll revisit the X11 version to see if I can get better text rendering with freetype2 or something in only a few lines of code.
Topic archived. No new replies allowed.