I have a c++ program that generates random files filled with gibberish, but for it to work it needs to run in the background. The method I am using generates a null window. I have made other programs using this background method, but it doesn't work in this program:
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include <fstream>
#include <windows.h>
using namespace std;
string random(int len)
{
string a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string r;
srand(time(NULL));
for(int i = 0; i < len; i++) r.push_back(a.at(size_t(rand() % 62)));
return r;
}
int main(){
restart:
/*This is the background code*/
HWND window;
AllocConsole();
window - FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(window, 0);
std::string file=random(1);
std::ofstream o(file.c_str());
o << random(999) << std::endl;
goto restart;
return 0;
}
I am using the dev C++ compiler
I just realized my problem, the goto statement needed to not include the null window rendering part, so the window wasn't re-rendered and de-rendered every time. Also there was a - that needed to be an =.
Related
I am trying to make a fun program where it display random numbers, but I need to remove the scrollbar so it looks more convincing. I managed to make the program full screen but I can't remove the vertical scrollbar. Screenshot
Code:
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, 0);
int output;
bool done = false;
system("color a");
while (!done) {
output = 1 + (rand() % (int)(1000 - 1 + 1));
cout << output;
}
}
There are many ways, one of them is manipulating the size of the internal buffer of the console to have the same size of the window and then using ShowScrollBar function to remove the scrolls.
#include <iostream>
#include <Windows.h>
#include <WinUser.h>
using namespace std;
int main() {
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, 0);
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hstdout, &csbi);
csbi.dwSize.X = csbi.dwMaximumWindowSize.X;
csbi.dwSize.Y = csbi.dwMaximumWindowSize.Y;
SetConsoleScreenBufferSize(hstdout, csbi.dwSize);
HWND x = GetConsoleWindow();
ShowScrollBar(x, SB_BOTH, FALSE);
int output;
bool done = false;
system("color a");
while (!done) {
output = 1 + (rand() % (int)(1000 - 1 + 1));
cout << output;
}
}
Another way is to rely on conio.h or another C/C++ header/library which implements user interface functions.
I'm sorry if this question is too simple for you, but i don't have good programming skills and ROS knowledge. I have a ROS topic in which are published some numbers that are heart beat intervals in seconds. I need to subscribe to that topic and do this kind of elaboration: The idea is to have a little array of ten numbers in which i can store continuously ten heart beat. Then i have a bigger array of 60 numbers that must go up by ten position in order to have at the bottom the newest ten values of the small array and it has to "throw away" the ten oldest values ( i did a bit of research and maybe i have to use a vector instead of an array because in C++ array are fixed as far as i read ). Then i have to print every time these 60 values in a text file (i mean in a loop, so the the text file will be continuously overwritten). Moreover, i see that ROS outputs the data from a topic in this manner: data: 0.987 with every data divided from the others by --- in a column. What i really want, because i need it for a script that reads text file in this manner, is a text file in which the values are in one column without spaces and other signs or words, like this:
0.404
0.952
0.956
0.940
0.960
I provide below the code for my node, in which, for now, i did only the subscribing part, since i have no idea on how to do the things that i have to do later. Thank you in advance for your help!!!
Code:
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
int main(int argc, char **argv)
{
ros::init(argc, argv, "writer");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/HeartRateInterval", 1000);
ros::spin();
return 0;
}
NOTE: I didn't include the Float32/64 header because i publish the heart beats as a string. I don't know if this is of interest.
EDIT: I will include below the code of the publisher node which publish on the ROS topic the data.
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main(int argc, char **argv)
{
ros::init(argc, argv, "heart_rate_monitor");
ros::NodeHandle n;
ros::Publisher pub = n.advertise<std_msgs::String>("/HeartRateInterval", 1000);
ros::Rate loop_rate(1);
while (ros::ok())
{
ifstream inputFile("/home/marco/Scrivania/marks.txt");
string line;
while (getline(inputFile, line)) {
istringstream ss(line);
string heart;
ss >> heart;
std_msgs::String msg;
msg.data = ss.str();
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
}
}
return 0;
}
Since what is published is the "variable" msg, i tried to replace in the code given as an answer the variable string_msg with msg, but nothing has changed. Thank you!
I'm not sure I understood exactly what you want but here is a brief example which might do what you need.
I'm using here an std::deque to have a circular buffer of 60 values. What you are missing in your code is a callback function process_message which is called for the subscriber every time a new message arrives.
I did not compile this code, so it may not compile right away but the basics are there.
#include <ros/ros.h>
#include <std_msgs/String.h>
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <deque>
static std::deque<std::string> queue_buffer;
static int entries_added_since_last_write = 0;
void write_data_to_file()
{
// open file
std::ofstream data_file("my_data_file.txt");
if (data_file.is_open())
{
for (int i = 0; i < queue_buffer.size(); ++i)
{
data_file << queue_buffer[i] << std::end;
}
}
else
{
std::cout << "Error - Cannot open file." << std::endl;
exit(1);
}
data_file.close();
}
void process_message(const std_msgs::String::ConstPtr& string_msg)
{
// if buffer has already 60 entries, throw away the oldest one
if (queue_buffer.size() == 60)
{
queue_buffer.pop_front();
}
// add the new data at the end
queue_buffer.push_back(string_msg.data);
// check if 10 elements have been added and write to file if so
entries_added_since_last_write++;
if (entries_added_since_last_write == 10
&& queue_buffer.size() == 60)
{
// write data to file and reset counter
write_data_to_file();
entries_added_since_last_write = 0;
}
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "writer");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/HeartRateInterval", 1000, process_message);
ros::spin();
return 0;
}
I decided to take the plunge in C++ after using Python for a couple of years now. Right now, I am trying to make a program that flashes LEDs on my keyboard every 60 minutes to remind me to stretch using their keyboard SDK. The flashing and timing work, but the problem is closing the program when you don't want or need it open.
I can exit the program before I first call the alarm() function. After I call the that alarm function, I get the Windows pop saying that I can either Abort, ignore, or retry with the code R6010.
I currently have it setup to try to catch a Ctrl-C event and end the while loop, but that ends with the same error anyways.
#include "stdafx.h"
#include <iostream>
#include "Windows.h"
#include "SDKDLL.h"
#include <string>
#include <chrono>
#include <thread>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctime>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
BYTE r = 0;
BYTE g = 0;
BYTE b = 0;
bool exiter = false;//Exit variable
void my_handler(int s){// Tells me when CTRL C was pressed
printf("Caught signal %d\n", s);
::exiter = true;
}
void alarm(){
//Basically turns on and off LEDs 5 times
for (int x = 1; x < 5; x++){
Sleep(500);
r = 255;
g = 255;
b = 255;
SetFullLedColor(r, g, b);
Sleep(500);
r = 0;
g = 0;
b = 0;
SetFullLedColor(r, g, b);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
signal(SIGINT, my_handler);//Trying to find a way for the program to exit nicely, this way uses CTRL C
SetControlDevice(DEV_MKeys_M_White);
while (::exiter == false){
sleep_until(system_clock::now() + seconds(4));//waits X seconds to do the sequence again
EnableLedControl(true);//Allows for control of LED
alarm();//Turns off and on the LEDS
EnableLedControl(false);//Disables the LEDs
}
return 0;
}
I have been researching this error for a couple hours with no solution working. Any help would be greatly appreciated!
I'm trying to run a bit of code to add trackbars onto some video, it's from the Learning OpenCV Second Edition book, but I can't compile my code and gives the error "namespace cv has no member CAP_PROP_POS_FRAMES"
Here's the first bit of the code
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include <fstream>
using namespace std;
int g_slider_position = 0;
int g_run = 1, g_dontset = 0; //start out in a single step mode
cv::VideoCapture g_cap;
void onTrackbarSlide(int pos, void *) {
g_cap.set(cv::CAP_PROP_POS_FRAMES, pos);
if(!g_dontset)
g_run = 1;
g_dontset = 0;
}
It's CV_CAP_PROP_POS_FRAMES (note the S) and it should be brought in by highgui.hpp. It's an unnamed enum in the global namespace.
So I have a program that pulls random images from a folder and creates a collage out of them, and set it to the windows wallpaper. Which seems to work fine. So I thought I would put in a sleep timer and let it automatically update itself without me having to run it every half hour or what ever. I did that and it works great, but I ran into a problem of a memory leak that wasn't noticed before I started looping it. I am attempting to dispose of the GDI+objects, but I keep getting the error that dispose is not a member of GDIplus::Image
I am loading an picture into an Image object, then resizing it and putting it into an array of Images, then I would like to dispose of the first Image. I would then like to dispose of the array after I finish working with the images in it.
This is being done with an old copy of VS2005.
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
#include <string>
#include <iostream>
#include <vector>
#include <dirent.h>
#include <time.h>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include "cwp05rnd.h"
using namespace Gdiplus;
using namespace std;
#pragma comment (lib,"Gdiplus.lib")
#pragma comment (lib, "user32.lib")
int main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
HDC hdc;
Graphics graphics(hdc);
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
CLSID jpegClsid;
GetEncoderClsid(L"image/jpeg", &jpegClsid);
SetCurrentDirectoryA("E:\\Photos");
ofstream outfile;
outfile.open ("outimgs.txt");
ofstream outfile2;
outfile2.open("imgpos.txt");
srand(time(NULL));
init_genrand(time(NULL));
vector<string> dirlist;
DIR *d;
struct dirent *dir;
int i=0;
d=opendir(".");
if (d)
{
while ((dir=readdir(d)) != NULL)
{
i++;
dirlist.push_back(dir->d_name);
}
closedir(d);
}
Image wp(L"E:\\Dropbox\\Photos\\wallpaper.jpg");
Graphics* wpimage = Graphics::FromImage(&wp);
int r;
int rvsize=100;
int rv[100]={0};
string img;
std::wstring wimg;
const wchar_t* rimg;
double cwidth;
double cheight;
double ratio;
int nheight;
int counter=0;
int full = 0;
int tries = 0;
int hfull = 0;
int imgnum =0;
int last=0;
Image* newpic[10];
while ( tries <10)
{
redo:
tries++;
int newrv=0;
while (newrv ==0)
{
r=genrand_int32()%i;
for (int k=0; k < rvsize; k++)
{
if (rv[k] > 0 && rv[k]==r )
{
break;
}
if (rv[k]==0 && r < i)
{
newrv =1;
rv[k]=r;
last=k;
break;
}
if (rv[k] ==0)
{
break;
}
}
}
img = dirlist[r];
if (img[0]=='.')
{
newrv=0;
goto redo;
}
wimg = std::wstring(img.begin(),img.end());
rimg = wimg.c_str();
Image pic(rimg);
cwidth = pic.GetWidth();
cheight = pic.GetHeight();
if (cheight ==0)
{
outfile2 << "error" << img << endl;
rv[last]=0;
system("pause");
goto redo;
}
ratio = cwidth/cheight;
nheight = nwidth/ratio;
pic.RotateFlip(Rotate180FlipNone);
pic.RotateFlip(Rotate180FlipNone);
newpic[imgnum] = pic.GetThumbnailImage(nwidth,nheight,NULL,NULL);
delete pic[0];
imgnum = imgnum + 1;
}
then there is a long section of flips and rotates on the images in newpic, according to various random values.
wpimage->DrawImage(newpic[k],(j*nwidth),(((k+1)*whitespace)+htot),nwidth,nh[k]);
wp.Save(L"C:\\Temp\\wallpaper\\nwallpaper.jpg", &jpegClsid, NULL);
delete newpic;
setWall();
delete wpimage;
delete wp;
return 0;
}
When I attempt to delete the Image objects, I get an error that says either it can not delete objects that are not pointers, or it cannot convert from GDIplus::Image to void*
Any advice would be appreciated.
I noticed you have Image pic(rimg);
But you are doing delete pic[0];
pic is not a pointer.. not dynamically allocated or something... nor is a array (or maybe it is.. but intuitively I think no..)
* Add *
Oh yeah, if you already solved this, suggests you close the question or at least mention it...