Cycle Through URLs - c++

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.

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.

Related

How to run PS1(powerShell Script) files with C++

I am making a toast notification apear, but when the script runs it just closes the powershell like if it ran it but no noticfication apears. How could I make it work?
#include<iostream>
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string>
#include <cstdlib>
using namespace std;
void main()
{
string strPath = "d:\\callPowerShell.ps1";
system("start powershell.exe d:\\callPowerShell.ps1");
}
It would recall the file and run it on PowerShell
Some time ago I made an app in C++Builder and I did something like that for made a backup in MariaDB. The code I used was something like this:
UnicodeString usMysqldump, usCmd;
usMysqldump = "In my case path to mysqldump.exe";
usCmd = "Script to be executed";
ShellExecuteW( Application->Handle, "open", usMysqldump, usCmd, NULL, SW_HIDE );
PD: SW_HIDE is for keep the console hide, may be you want something diferent.
Try this code and I hope it be helpfull for you.

Error in ofStream, Qt 5.14.2 can 't write string into file

I am trying to write my program from VS (MVCS) for Qt(MinGb)
I am using ofstream and has a following code:
#include <fstream>
#include <string>
#include <iostream>
#include <exception>
using namespace std;
void WriteToFile(ofstream* fileToWrite, std::string StringNeedsToWrite)
{
if (fileToWrite)
{
if (IsStartOfNewString(StringNeedsToWrite))
{
*fileToWrite << '\n';
}
*fileToWrite << StringNeedsToWrite;
}
else
{
throw exception();
}
}
I want to write string into file, and set ofstream into my method.
But i have a strange error and don't know how to fix it (on image)
They differ in their make files and project files. A common problem is that moving a project from one environment to another entails big mistakes.
For example, MinGW under Linux does not have standard streams, but there is windows.h.
Such problems should be taken into account and studied at the stage of developing a program architecture and choosing technologies for development.
Frequent practice has shown that Qt is best used with your IDE and, if possible, use its classes for development.

First Instance of "STD_OUTPUT_HANDLE" Identifier is Undefined

I am using Visual Studio 2019, and my code uses console outputs which change colors frequently. I am including Windows.h in my code, which is the header file that contains SetConsoleTextAttributes, whereas STD_OUTPUT_HANDLE should be initialized by using namespace std. My code in its entirety can be found here, but the following is the section with the error:
#include <iostream>
#include <cmath>
#include "HeadFile.h"
#include <windows.h>
#include <string.h>
using namespace std;
int Play(char(&spaces)[7][6], int(&color)[7][6], int player, int playerOneWins, int playerTwoWins, int ties)
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
.....
The code runs fine, but inside of studio itself, I see the following error:
The error is coming from the first instance of STD_OUTPUT_HANDLE only (another case at the bottom of the picture has no errors). If I comment out the first one, the next instance errors:
How can I fix this issue? I've read in a few non-related posts that using namespace std can sometimes lead to problems. Is this the case?
Use Header file "Windows.h" instead of "windows.h".
I had the same problem, adding #include <stdlib.h> fixed it for me.
I had the same problem, you need to use #include <Stdio.h>

how to change the default code template in Xcode

I am using Xcode to code C++. Whenever I create a new project, the default code template is
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
I want to change it into something else. How can I do this?
Thanks in advance.
So emm.. thanks to the encouragement of #Alan Birtles. I now know how to do this.
First, we need to find where the default templates are. Go to this directory
/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/Project\ Templates/Mac/Application/Command\ Line\ Tool.xctemplate
I use Xcode to mostly code C++ codes, so I am only changing the command line tool templates. But I think this works for all kinds of tools.
Now that you are in this directory, you should see a file named:
TemplateInfo.plist
This is the file you should change.
Open this file with a test editor. In my case, I used atom to make changes to this file.
This file is written in a special syntax(that I didn't understand)
Try to understand the basic syntax and change this as the way you like.
Here's how I changed the default C++ part:
<key>C++</key>
<dict>
<key>Nodes</key>
<array>
<string>main.cpp:comments</string>
<string>main.cpp:include</string>
<string>main.cpp:main:content</string>
</array>
<key>Definitions</key>
<dict>
<key>main.cpp:include</key>
<string>#include <iostream> //changes start from here
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iostream>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
#include <queue>
using namespace std;//changes end here
</string>
<key>main.cpp:main:content</key>
<string>// insert code here...
std::cout << "Hello, World!\n";
return 0;
</string>
</dict>
</dict>
Save the changed file and try to create a new command line project.
It works great.
ps.Do copy the file before you make any changes in case you mess it up, if you changed it in the wrong way and didn't make a copy of that file, there's a chance that you won't be able to create a project.

Storing result in a file in C++

I'm writing a program to ping an IP.
I must ping a specific address for 1000s.
Now I want save the TTL to a file to draw it's histogram.
How I can do it? How can I just save the TTL to file?
Here's what I've tried:
#include "stdafx.h"
#include "iostream"
#include "string.h"
#include "stdlib.h"
#include "conio.h"
#include <string>
#include <windows.h>
#include <iostream>
#include <fstream>
using namespace std;
static string host;
static string ping_again;
void ping()
{
cout << "Host: ";
host="www.yahoo.com";
system (("ping " + host).c_str);
}
int main()
{
ping();
return(0);
}
You've chosen to run the ping executable directly with system() and what you are missing is the capture of the output of the command, so you can parse the TTL.
Capturing stdout could have been done with popen instead of system, however, from your list of includes you seem to be on Windows, and that’s where it gets complicated. Check out this question: What is the equivalent to Posix popen() in the Win32 API?, and note that you can call popen if you application is a console application, otherwise the answers will refer you to an MSDN example which painfully details how you should invoke a process and redirect its input/output.
Once you redirect and capture the output you should parse the strings to extract the TTL. Note that the output of ping.exe may be different for different versions of Windows, and that you have little control what ping.exe you are actually invoking.
The alternative and the better approach is to use the ICMP APIs directly instead of invoking a ping executable on hosts where your application runs. Start with IcmpSendEcho and note that it provides the RTT in the reply structure.