This question already has an answer here:
C++/SQLite only outputting one row of data
(1 answer)
Closed 5 years ago.
I'm having an issue where only one of my 2 test rows will output in C++. My code is:
#include <cstdio>
#include <sqlite3.h>
#include <windows.h>
#include <wincrypt.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
/*Definitions*/
sqlite3 *db;
void *arg;
char *err;
const char* stmt = "SELECT * from table";
/*End of Definitions*/
int exec(void *arg, int argc, char **argv, char **column) {
int i;
for(i = 0; i < argc; i++) {
cout << column[i] << ": " << argv[i] << endl;
}
cout << "------" << endl;
}
int main() {
int rc = sqlite3_open("test.sqlite", &db); /*Open db "test.sqlite"*/
if(!rc) {
while(true) {
sqlite3_exec(db, stmt, exec, arg, &err);
if(err) {
break;
}
}
}
/*Ending Stuff (NOTHING BEYOND THIS POINT)*/
cin.get();
return 0;
}
I am not getting any errors; it is purely just outputting the first row. cout << err << endl; will not output anything. Any help is appreciated, thanks. Also, I made a post about this 2 days ago but due to me not receiving help, I am reposting. #CL. happened to make a comment on my previous post saying my SQL was incorrect but I am still yet to figure out what he meant by that
sqlite3_exec () is used to execute insert update statements. For select use sqlite3_prepare (...)
Related
I'm new to thread programming. I'm trying to create an application which continually checks the Last Modification time of some file and exits the program when that time has changed.
Please find my code below:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <cerrno>
#include <unistd.h>
using namespace std;
#define NUM_THREADS 2
void *getFileCreationTime(void *path) {
const char *pt;
pt=(const char *)path;
struct stat attr;
stat("/home/utthunga/shmrp.cpp", &attr);
while(1){
char *timestamp= ctime(&attr.st_mtime);
if(timestamp)
{
cout<<"Last modified time: %s"<< ctime(&attr.st_mtime)<<endl;
cout<<"No changes has been made to the file"<<endl;
sleep(4);
}
else
{
cout<<"Last modified time: %s"<< ctime(&attr.st_mtime)<<endl;
cout<<"Time stamp has been changed"<<endl;
exit(0);
}
}
pthread_exit(NULL);
}
int main()
{
pthread_t threads[NUM_THREADS];
int i;
int rc;
for( i = 0; i < NUM_THREADS-1; i++ )
rc = pthread_create(&threads[i], NULL, getFileCreationTime, (void *)i);
pthread_exit(NULL);
return 0;
}
Can anyone please tell me what changes I have to implement in order to check the last modification time of that file continually and exit the application when that time has changed?
After you retrieve the file's modification time the first time, you need to save it so you can compare it to subsequent values retrieved afterwards.
Try something more like this instead:
void* getFileCreationTime(void *) {
const char *path = "/home/utthunga/shmrp.cpp";
struct stat attr;
if (stat(path, &attr) < 0) {
cout << "stat error" << endl;
exit(0);
}
time_t mtime = attr.st_mtime;
cout << "Last modified time: " << ctime(&mtime) << endl;
while(1) {
sleep(4);
if (stat(path, &attr) < 0) {
cout << "stat error" << endl;
exit(0);
}
if (attr.st_mtime != mtime) {
cout << "Time stamp has been changed" << endl;
exit(0);
} else {
cout << "No changes have been made to the file" << endl;
}
}
pthread_exit(NULL);
}
I'm having an issue where only one of my 5 test rows will output in C++. My code is:
#include <cstdio>
#include <sqlite3.h>
#include <windows.h>
#include <wincrypt.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
/*Definitions*/
sqlite3 *db;
void *arg;
char *err;
const char* stmt = "SELECT * from table";
/*End of Definitions*/
int exec(void *arg, int argc, char **argv, char **column) {
int i;
for(i = 0; i < argc; i++) {
cout << column[i] << ": " << argv[i] << endl;
}
cout << "------" << endl;
}
int main() {
int rc = sqlite3_open("test.sqlite", &db); /*Open db "test.sqlite"*/
if(!rc) {
while(true) {
sqlite3_exec(db, stmt, exec, arg, &err);
if(err) {
break;
}
}
}
/*Ending Stuffz (NOTHING BEYOND THIS POINT)*/
cin.get();
return 0;
}
I am not getting any errors; it is purely just outputting the first row. Any help is appreciated, thanks.
Just return 0 in the end of exec function
Explanation:
In function 'exec" when it return nothing, the database server acts like the function is terminated by somehow (like something corrupt the function and made an error) so it just stop sending data because the call back function terminated with an error.... so when we return 0 we say that the function "exec" is working normally so it keeps sending data ... and if you return any other integer it will deal with it like the function terminated by an error and stop sending data. I faced the same issue and I solved it by making the callback function (exec) return 0.
I think it might be something related to the way my pointers are used/initialized in my struct, but I'm not completely sure. I use 'g++ -lpthread main.cpp' to compile. The program just hangs in Linux, while executing properly in windows. The program doesn't even spit out a cout I put in the beginning of the code for debugging purposes.
#include "pthread.h"
#include "semaphore.h"
#include "time_functions.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct vars {
char buffer[10][1000];
int put;
int take;
sem_t openSlot;
sem_t slotInactive;
sem_t newData;
ifstream readFile;
ofstream writeFile;
};
void *write(void *in) //consumer, writes data to file and deletes data from buffer
{
vars* writeVars = (vars*)in;
while (true)
{
sem_wait(&(*writeVars).newData);
sem_wait(&(*writeVars).slotInactive);
if ((*writeVars).buffer[(*writeVars).take % 10][0] != '$')
{
(*writeVars).writeFile << (*writeVars).buffer[(*writeVars).take % 10];
if ((*writeVars).readFile.eof() != true)
{
(*writeVars).writeFile << endl;
}
else
{
break;
}
}
(*writeVars).take++;
sem_post(&(*writeVars).openSlot);
sem_post(&(*writeVars).slotInactive);
}
pthread_exit(0);
return 0;
}
void *read(void *in) //producer, reads data into buffer
{
vars* readVars = (vars*)in;
char read_line[1000];
while ((*readVars).readFile.getline(read_line, 1000))
{
sem_wait(&(*readVars).openSlot);
sem_wait(&(*readVars).slotInactive);
strcpy((*readVars).buffer[(*readVars).put % 10], read_line);
(*readVars).put++;
sem_post(&(*readVars).slotInactive);
sem_post(&(*readVars).newData);
}
sem_wait(&(*readVars).openSlot);
sem_wait(&(*readVars).slotInactive);
(*readVars).buffer[(*readVars).put % 10][0] = '$';
sem_post(&(*readVars).slotInactive);
sem_post(&(*readVars).newData);
pthread_exit(0);
return 0;
}
int main(int argc, char* argv[])
{
char pause[10];
vars *varsPointer, var;
varsPointer = &var;
var.take = 0;
var.put = 0;
var.writeFile.open(argv[2], ios::out);
var.readFile.open(argv[1], ios::in);
start_timing();
sem_init(&var.openSlot, 0, 10);
sem_init(&var.slotInactive, 0, 1);
sem_init(&var.newData, 0, 0);
pthread_t read_Thread, write_Thread;
pthread_create(&read_Thread, NULL, read, varsPointer);
pthread_create(&write_Thread, NULL, write, varsPointer);
pthread_join(read_Thread, NULL);
pthread_join(write_Thread, NULL);
sem_destroy(&var.openSlot);
sem_destroy(&var.slotInactive);
sem_destroy(&var.newData);
stop_timing();
var.readFile.close();
var.writeFile.close();
//Display timer
cout << "wall clock time (ms):" << get_wall_clock_diff() * 1000 << '\n';
cout << "cpu time (ms):" << get_CPU_time_diff() * 1000 << '\n';
cout << "Type Something and Press Enter To Continue";
cin >> pause; //Just used to keep cmd promt open in Windows after program execution
return 0;
}
im trying to communicate a pthread with a process, using pipes, for a college proyect. i make a struct with the pipes and i pass that structure to the pthread so it can listen on the pipe[0], and on the rest of the code i try to send a string to that running pthread.
Here is my code:
#include <unistd.h>
#include <string>
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <pthread.h>
using namespace std;
struct Pipefd{
int pipe[2];
string name;
};
void* listenProcess(void* x){
Pipefd* pf = reinterpret_cast<Pipefd*>(x);
close(0);
dup(pf->pipe[0]);
//here i try to see if the struct i send is ok, but this is not printed.
cout << "pf.name: " << pf->name << endl;
string recive;
while(getline(cin,recive)){
cout << "recive: " << recive << endl;
}
cout << "Problem with getline" << endl;
}
int main(int argc, char *argv[]) {
Pipefd myPipe;
myPipe.name = "Test";
pipe(myPipe.pipe);
void* test = reinterpret_cast<void*>(&myPipe);
pthread_t tid;
pthread_create(&tid,NULL, &listenProcess,test);
close(1);
dup(myPipe.pipe[1]);
cout << "This is a message" << endl;
pthread_join(tid,NULL);
}
if someone can reply me with some ideas of how to make this work it would be awesome, if not, thank you for your time.
I was wondering if you could have it so when you go and click on a program in linux it always automatically brings up the command line for the information being displayed or if I decided to use ncurses for an interface. If so is this a system specific call or can you do this with ncurses? Because half of my program is going to be via terminal.
Thanks
Since nitt wouldn't let me amend his code snippet, I'm posting a corrected snippet in case anyone would like to use it:
#include <cstdio>
#include <unistd.h>
#include <iostream>
int main(int argc, char* argv[])
{
if (isatty(0))
{
std::cout << "Hello, World!" << std::endl;
for (int i=0; i<argc; i++)
std::cout << "arg: " << i << "\t" << argv[i] << std::endl;
std::cout << "Press return to continue . . ." << std::flush;
std::cin.get();
}
else
{
const char* args[argc+3], **it=args;
*it++ = "gnome-terminal";
*it++ = "-x";
it = std::copy(argv, argv+argc, it);
*it++ = 0;
if (-1 == execvp("gnome-terminal", (char* const*) &args[0]))
perror("exec");
}
}
Yes, just invoke a terminal with your app in it. For example:
rxvt -e myapp
Starts a terminal running your app. You could also use xterm. If you want to use wide chars/unicode I recommend rxvt-unicode.
You can put this in a .desktop file with an icon defined there, and then that will be placed in the system menu.
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int fileExists(string x321) {
ifstream x123 (x321.c_str());
string x213;
x123 >> x213;
if (x213 == "") {
return false;
} else {
return true;
}
}
int createConsole(string fname) {
if (fileExists("~tmp") == false) {
ofstream tmp ("~tmp");
tmp << "tmpfile";
fname = "gnome-terminal -e " + fname;
system(fname.c_str());
system("exit");
return 0;
}
remove("~tmp");
return 1;
}
int main(int argc, char** args) {
createConsole(args[0]);
cout << "Hello, World!" << endl;
cout << "Press return to continue . . .";
cin.get();
}
Pay attention to the "createConsole" and "fileExists" function. I wrote this myself.