0

I am writing a small program for my father. He needs it to cycle through a list of URLs contained in a text file every 15 seconds. I have it opening the URLs but I cant figure out how to either redirect the current tab or close and open a new one.

#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <string>
#include <fstream>
#include <iostream>
#include <windows.h>

std::ifstream infile("links.txt");

int main(){
    std::string link;

    while(std::getline(infile, link)){
        system(std::string("start " + link).c_str());
        Sleep(15000);
    }
}

I am fairly inexperienced with C++ and out of practice. Any and all help is greatly appreciated.

Kieran
  • 23
  • 3
  • Every 15 seconds seems very fast, `system` won't return until the started process exits (so won't work like you think), and I don't know that you can easily do this from a command line application. – crashmstr May 26 '15 at 19:12

1 Answers1

0

Your first issue is you are calling start on a link which does not exist as a program, this won't work if that is the link to a website. That being said, using system() is very dangerous and slow, this explains it a little better and provides an alternative for windows: https://stackoverflow.com/a/15440094/2671276.

Community
  • 1
  • 1
Spartan322
  • 43
  • 9
  • ok so I have figured out the CreateProcess now how do I close it to open another? – Kieran May 28 '15 at 00:07
  • Well you can checkout [TerminateProcess()](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686714(v=vs.85).aspx) for windows but you need to save the handle and not destroy it. Also terminating a program like this is rarely ever a good idea (doing things like this could at worst lead to a memory leak) – Spartan322 May 28 '15 at 09:21