Hello, my name is Sandra.
I have been bothering myself for few days with this issue.
So I'm trying to create a code that will read from text file named "files.txt" and paste the 2nd line from that text file to somewhere else, like an opened window.

void ReadFiles ()
  ifstream read ( "files.txt" );

   string my_string;

   int counter = 0;
   while (getline(read, my_string, '\n'))
      if (counter ==1 )

     counter = counter + 1;


I'm using that line of code to read 2nd line from my text file, and save it as my_string, and I have no problem with it, it works correctly.
Part that bothers me is that I have for an example opened empty notepad file, and when I execute this program I want it to just paste, the my_string text, the part which he took from 2nd line of text file.
In my case I don't want it to paste it into notepad file, just to paste it into one username section box on one application.
I tried with saving the string to clipboard and then using SendKeys to send CTRL+V, but it didn't end up working.

Can you please help?

Edited 4 Years Ago by SandraD

I think you may be using the wrong language for this. It's certainly doable, but there
are much better alternatives (at least on windows). One such alternative is Autoit.

Here's an example that does what you want with notepad:

$file = FileOpen("C:/in.txt")
$line2 = FileReadLine($file, 2)
ControlSend("[CLASS:Notepad]", "", "Edit1", $line2)

Edited 4 Years Ago by m4ster_r0shi

I know :)
It is really simple in autoit and other macro programs, in which I made it without problems :)
But I really want to learn about it in c++, because I would like to improve myself in that language :)
I'm sorry if I asked too hard question, I'm just strugling with it atm so I felt my weakness.

Oh, well, I was hoping I could avoid this...

#include <windows.h>
#include <iostream>
#include <fstream>

using namespace std;

BOOL CALLBACK printClassName(HWND hwnd, LPARAM lParam)
    CHAR buffer[512];

    GetClassNameA(hwnd, buffer, 511);

    cout << buffer << endl;

    return TRUE;

int main()
    ifstream fin("C:/in.txt");

    string line2;

    getline(fin, line2);
    getline(fin, line2);

    string className;

    cout << "window class names: " << endl;
    EnumWindows(printClassName, NULL);

    cout << "enter window class name: ";
    getline(cin, className);

    HWND hwnd = FindWindow(className.c_str(), NULL);
    if (hwnd == NULL)
        cout << "couldn't find window...";
        cin.get(); return 0;
    cout << "got it!" << endl;

    cout << "control class names: " << endl;
    EnumChildWindows(hwnd, printClassName, NULL);

    cout << "enter control class name: " << endl;
    getline(cin, className);

    HWND control = FindWindowEx(hwnd, NULL, className.c_str(), NULL);
    if (control == NULL)
        cout << "couldn't find control...";
        cin.get(); return 0;
    cout << "got it!" << endl;

    SendMessage(control, WM_SETTEXT, 0, (LPARAM)line2.c_str());


After opening notepad, I ran the above, entered "Notepad", then "Edit", and it worked as expected.

Edited 4 Years Ago by m4ster_r0shi

But I really want to learn about it in c++, because I would like to improve myself in that language :)

Why is it people always want to "improve myself" doing things that the language makes it difficult to do and can only be done in rare cases in the first place. What you want to do will only work on the operating system you learn it on -- so it's use is limited and questionable.

To "improve myself", try doing things the language is meant to do, and is easily portable from system to system, compiler to compiler. If you are coding in a non-standard non-portable way, learn the standard portable way to program to "improve myself".

This article has been dead for over six months. Start a new discussion instead.