Visual C++ Error: Debug Assertion Failed - c++

Hello i compliled my c++ program and if i start the .exe i got a error
Image from the error
This is my source(main.cpp):
#include <iostream>
#include <string>
#include <Windows.h>
#include <dos.h>
#include <stdio.h>
#include <fstream>
#include <ctime>
// using
using namespace std;
bool fexists(const char *filename);
int main() {
try {
HANDLE h;
string cClipboard = "";
CreateDirectory("C:\\Program Files\\Clipboard Logger", NULL);
if (!fexists("C:\\Program Files\\Clipboard Logger\\log.txt")) {
FILE *fp;
fp = fopen("C:\\Program Files\\Clipboard Logger\\log.txt", "w");
fclose(fp);
}
while (true) {
if (!OpenClipboard(0)) {
Sleep(2000);
continue;
}
h = GetClipboardData(CF_TEXT);
CloseClipboard();
if ((char *)h == cClipboard) {
Sleep(2000);
continue;
}
cClipboard = (char *)h;
time_t t = time(0);
struct tm * now = localtime(&t);
FILE *fp;
fp = fopen("C:\\Program Files\\Clipboard Logger\\log.txt", "a");
int day = now->tm_mday;
int month = now->tm_mon + 1;
int year = now->tm_year + 1900;
int sec = now->tm_sec;
int min = now->tm_min;
int hour = now->tm_hour;
char logLine[sizeof((char *)h) + 64];
sprintf(logLine, "%d.%d.%d %d.%d.%d %s\n", hour, min, sec, day, month, year, (char *)h);
fprintf(fp, (char *)logLine);
fclose(fp);
cout << (char *)logLine << endl;
Sleep(2000);
}
getchar();
return 0;
} catch (...) {
}
}
bool fexists(const char *filename) {
ifstream ifile(filename);
if (ifile)
return true;
return false;
}
iam new in c++ and i dont know how to fix it, because if i debug the program all works fine but with the exe it doesnt work.

Related

multithreaded file copying

I wrote a c++ code to multithreaded copying a file to another directory in linux. but doesn't work(just it made an empty file in directory).
I don't know what's the problem? I think my tread has no right access to write in the shared file. but don't know what should I do.
It should work when typed in terminal :
$./a.out <file name> <dir> <thread number (default 4)>
This is my code:
/*Multithreads file copier*/
#include <fcntl.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <sys/sendfile.h>
#include <unistd.h>
#include <cstdio>
char* file;
char* fileout_path;
int fin, fout;
// part of each thread
struct PART{
off_t* offset =0;
size_t size;
};
//multithreading
void *Copy (void * data)
{
struct PART *mypart;
mypart = (struct PART *) data;
//open file to read and write
fin = open(file, O_RDONLY,0);
fout = open(fileout_path, O_WRONLY|O_CREAT, 0644);
unsigned long a = static_cast<unsigned long>(mypart->size);
lseek(fout, a, SEEK_SET); //set offset by size of the part
//use sendfile instead read and write to easier code
sendfile(fin, fout, mypart->offset, mypart->size);
printf("threading....\n");//to know the thread ran
pthread_exit(0);
}
int main(int argc, char *argv[])
{
int threads_number;
if (argv[3]!= NULL)
{
threads_number = atoi(argv[3]);
}
else
{
threads_number = 4;//default thread number
}
//multithreading datatypes
pthread_t tid[threads_number];
pthread_attr_t attr;
pthread_attr_init(&attr);
struct stat f_stat;
struct PART part[threads_number];
//allocation size of each part
unsigned long part_size = f_stat.st_size / threads_number;
for(int i =0; i <number_threads; i++)
{
if ( i == threads_number -1)
{
part[threads_number].size = f_stat.st_size - (part_size * (threads_number -1));
}
else
{
part[i].size = part_size;
}
}
file = argv[1];
stat(file, &f_stat);
fileout_path = argv[2];
int fin1 = open(file, O_RDONLY,0);
int fout1 = open(fileout_path, O_WRONLY|O_CREAT, 0644);
for (int j = 0; j < threads_number; j++)
{
pthread_create(&tid[j], NULL, Copy, (void *)&part[j]);
pthread_join(tid[j],NULL);
}
printf("thread is done.\n");
close(fout);
close(fin);
return 0;
}

Trim an audio file that is labelled with a timestamp c++

So i have a program that records snippets of audio on a microphone array, it then timestamps the end time of the file. My problem is that the process of the program starting the recording takes a random amount of time to start and so the audio is a random length. I want a way to read the size of the file (kilobytes) then trim the file by a certain number of kilobytes so that the recordings are always the same length.
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <chrono>
#include <thread>
#include<signal.h>
#include<unistd.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include "zlib.h"
long long int rem;
long long int ms1;
long long int unixtime;
using namespace std;
const char* Filenames;
int main(void) {
int system(const char *command);
int x;
struct timeval tp1;
rem = 5;
while (rem != 0) {
gettimeofday(&tp1, NULL);
ms1 = tp1.tv_sec * 1000ll + tp1.tv_usec / 1000ll;
rem = ms1 % 10000;
}
for (x=0; x<3; x++){
pid_t pid=fork();
if (pid==0){
execl("/home/pi/odas/bin/odaslive", "/home/pi/odas/bin/odaslive", "-vc", "/home/pi/odas/config/odaslive/matrix_creator.cfg", (char *)NULL);
exit(127);
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(15000));
kill(pid, SIGINT);
}
gettimeofday(&tp1, NULL);
unixtime = tp1.tv_sec + tp1.tv_usec / 1000000ll;
std::string name1 = "/home/pi/matrix_creator_explore/postfiltered/postfiltered1_";
std::string name2 = ".raw";
std::string result1;
result1 = name1 + std::to_string(unixtime) + name2;
const char *cstr = result1.c_str();
rename ("/home/pi/matrix_creator_explore/postfiltered.raw", cstr);
std::string name3 = "/home/pi/matrix_creator_explore/tracked/tracked1_";
std::string name4 = ".raw";
std::string result2;
result2 = name3 + std::to_string(unixtime) + name4;
const char *cstr1 = result2.c_str();
rename ("/home/pi/matrix_creator_explore/tracked.raw", cstr1);
struct stat buf;
stat( cstr,&buf);
printf ("\n %i \n", buf.st_size);
}
}

Im unsure of why my http server is not reading in a filelocation C++

Im creating a http server in c++. I know that TCPServer.h works and that thing/text.txt exists but for some reason the code doesnt read the text file when I do 127.0.0.1. If you would like TCPServer.h to help help just ask I would be more than happy to send it but I just didnt see a reason to do it
#include "TCPServer.h"
#include <future>
#include <fstream>
#include <WS2tcpip.h>
#include <iostream>
#define MAX_BUF (4096)
#pragma comment (lib, "ws2_32.lib")
char * getLoc(char * c) {
char * ret = (char *)malloc(4096);
ZeroMemory(ret, 4096);
for(int i = 4; i < 4092; i++) {
if (*(c + i) == ' ') {
return ret;
}
else {
*(ret + i - 4) = *(c + i);
}
}
return ret;
}
void doStuff(SOCKET sock) {
char * recieved = (char *)malloc(4096);
recv(sock, recieved, 4096, 0);
char * loc = getLoc(recieved);
std::string fileLocation("thing");
fileLocation += loc;
std::ifstream fil;
fil.open(fileLocation);
char * contents = (char *)malloc(4096);
ZeroMemory(contents, 4096);
fil.read(contents, 4096);
fil.close();
std::cout << fileLocation;
std::string shoot("HTTP/1.1 200 OK\n");
shoot += contents;
std::cout << "\n\n\n" << shoot;
send(sock, shoot.c_str(), 4096, 0);
}
int main() {
TCPServ s;
s.createSock(80);
while (true) {
SOCKET sock = s.listenFor();
std::future <void> f = std::async(doStuff, sock);
}
}

String Vector showing duplicates C++

Not working Code:
#include "stdafx.h"
#include <stdio.h>
#include "sqlite3.h"
#include <Windows.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
std::vector<string> emailsfound;
static int callback(void *data, int argc, char **argv, char **azColName)
{
int i;
string thefile;
for(i=0; i<argc; i++)
{
thefile = string(argv[i]);
size_t found = thefile.find(":");
if(found != std::string::npos)
{
thefile.erase(thefile.begin(), thefile.begin()+1);
emailsfound.push_back(thefile);
//here's the problem
cout << emailsfound[i] << endl; //here it only couts emailsfound[0] over and over until the loop's work is done.
}
else
{
}
}
return 0;
}
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
const char* data = "Callback function called"; //I am not printing this
/* Open database */
rc = sqlite3_open("C:\\Users\\main.db", &db);
if( rc )
{
return 0;
}
else
{
}
/* Create SQL statement */
sql = "SELECT emails from People";
/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
if( rc != SQLITE_OK )
{
sqlite3_free(zErrMsg);
return 0;
}
else
{
}
sqlite3_close(db);
system("PAUSE");
return 0;
}
Working Code:
#include "stdafx.h"
#include <stdio.h>
#include "sqlite3.h"
#include <Windows.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
std::vector<string> emailsfound;
static int callback(void *data, int argc, char **argv, char **azColName)
{
int i;
string thefile;
for(i=0; i<argc; i++)
{
thefile = string(argv[i]);
size_t found = thefile.find(":");
if(found != std::string::npos)
{
thefile.erase(thefile.begin(), thefile.begin()+1);
emailsfound.push_back(thefile);
//Doing this makes it works great.
printthevector();
}
else
{
}
}
return 0;
}
void printthevector()
{
int sizeofthevector;
int i = 0;
sizeofthevector = emailsfound.size();
while (i < sizeofthevector)
{
cout << emailsfound[i].c_str() << endl; //print everything / it works great
}
}
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
const char* data = "Callback function called"; //I am not printing this
/* Open database */
rc = sqlite3_open("C:\\Users\\main.db", &db);
if( rc )
{
return 0;
}
else
{
}
/* Create SQL statement */
sql = "SELECT emails from People";
/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
if( rc != SQLITE_OK )
{
sqlite3_free(zErrMsg);
return 0;
}
else
{
}
sqlite3_close(db);
system("PAUSE");
return 0;
}
As you can see, in the first code it only counts emailsfound[0] over and over for some reason so I had to create a proper void to cout all the emails found properly.
Please explain this to me, I know I fixed it but I am not sure why the first code was not working.

sh not found when executing shell command from C++

I have a function in my C++ program like this:
std::string SysExec::executeAndReturnResult(char* cmd) {
//cout << "[SHELL] : " << cmd << endl;
FILE* pipe = popen(cmd, "r");
if (!pipe)
return "ERROR";
char buffer[128];
std::string result = "";
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
When I call it
SysExec::executeAndReturnResult("/usr/bin/irsend -d /var/run/lirc/lircd-lirc0 SEND_ONCE Samsung 4");
I get this:
sh: 4: not found
However, if I try to same command from the console, it works fine.
I would like to mention that calling it with a non-numeric argument works with no issue — the following works fine:
SysExec::executeAndReturnResult("irsend -d /var/run/lirc/lircd-lirc0 SEND_ONCE Samsung channel+")
I have tried Google..but to no avail. Can anyone help me understanding the issue?
//edit on request:
my SysExec.h file looks like this:
#include "CompileFlags.h"
/*
* sysexec.h
*
* Created on: Dec 19, 2012
* Author: Shrouk H. Khan / Fingi / root
*/
#ifndef SYSEXEC_H_
#define SYSEXEC_H_
#include<stdio.h>
#include <string>
#include <boost/algorithm/string.hpp>
namespace fingigcc {
class SysExec {
public:
SysExec();
virtual ~SysExec();
static std::string executeAndReturnResult(char* cmd);
static int executeAndForget(char* cmd);
static int execute(char* cmd);
};
} /* namespace fingigcc */
#endif /* SYSEXEC_H_ */
And the corresponding cpp file looks like this:
/*
* SysExec.cpp
*
* Created on: Dec 19, 2012
* Author: Shrouk H. Khan / Fingi / root
*/
#include "../../includes/SysExec.h"
#include "../../includes/Logger.h"
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
namespace fingigcc {
SysExec::SysExec() {
// TODO Auto-generated constructor stub
}
SysExec::~SysExec() {
// TODO Auto-generated destructor stub
}
std::string SysExec::executeAndReturnResult(char* cmd) {
//cout << "[SHELL] : " << cmd << endl;
FILE* pipe = popen(cmd, "r");
if (!pipe)
return "ERROR";
char buffer[128];
std::string result = "";
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
int SysExec::executeAndForget(char* cmd) {
//cout << "[SHELL] : " << cmd << endl;
//do not try to log it..infinite loop..
// pid_t pid = fork();
// if (pid < 0) {
// return -1;
// } else if (pid == 0) {
// execl(cmd, (char *) 0);
// } else {
//
// }
system(cmd);
return 0;
}
}
/* namespace fingigcc */