ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have written in C in the past, but I find C++ a teensy little bit different.
I have taken the gtk "Hello World" example and modified it a bit. instead of outputting to stdout like that example, i want to output to a file. (I can manage that).
However, I want to be able to pass in the filename as a parameter of the program. So I would run
Code:
MyProg SomeFile
and the result would be a file called SomeFile with my output in it.
How do I pick SomeFile from argv[1][0] and pass it through to the routine that picks up the button press, so that it knows what file to open.
(Yes, I intend to Open-write-close on each button press to ensure that my data is writen safely - it won't be a performance problem.
It would help if you provide what you have gotten so far. Fortunately for needs, GTK is in C.
You should already have char **argv in your [i]main[i] function. Setup a signal with that argv[1] or whatever one it is. [i]char *[i] or const char * can be converted to gpointer (which is void * typedefed). You handler (which is a C function pointer) can dereference the pointer to the pointer of the filename.
OK. Here is what I have. 3 files plus Compiler output.
BTW A smiley creeps in to the code snippets where I have a : followed by an o.
Code:
//$Id: ElTimeBody.h,
/*
*
* This is the .h file for the Elapsed Time module
*
*/
#ifndef GTKMM_ELAPSEDTIME_H
#define GTKMM_ELAPSEDTIME_H
#include <gtkmm/button.h>
#include <gtkmm/window.h>
class ElapsedTime : public Gtk::Window
{
public:
ElapsedTime();
virtual ~ElapsedTime();
void setFileName(char FileName) {strcpy(_FileName, FileName);}
private:
char _FileName[300];
time_t mtimeStartTime;
protected:
//Signal handlers:
virtual void on_button_clicked();
//Member widgets:
Gtk::Button m_button;
};
#endif // GTKMM_ELAPSEDTIME_H
Code:
//$Id: ElTimeBody.cc
/*
*
* This program is the Elapsed Time module.
* When instantiated, it creates a button and places it in the Window.
*
* When the button is pressed, it will calculate the elapsed time since the
* program started (in HH:MM:SS) and output that time.
* It is supposed to output to a file, whosw name is passed in as a parameter
* to the main program.
*
*/
#include "ElTimeBody.h"
#include <iostream>
#include <time.h>
time_t mtimeStartTime;
ElapsedTime::ElapsedTime()
: m_button("Press me at Significant Times") // creates a new button with the label "Press me at Significant Times".
{
// Sets the border width of the window.
set_border_width(30);
time( &mtimeStartTime );
// strcpy(szFileName, cFilename);
// When the button receives the "clicked" signal, it will call the
// on_button_clicked() method. The on_button_clicked() method is defined below.
m_button.signal_clicked().connect(sigc::mem_fun(*this, &ElapsedTime::on_button_clicked));
// This packs the button into the Window (a container).
add(m_button);
// The final step is to display this newly created widget...
m_button.show();
}
ElapsedTime::~ElapsedTime()
{
}
void ElapsedTime::on_button_clicked()
{
time_t ltimeTimeNow;
double ldETime;
long llETime;
int liSeconds, liMinutes, liMinsLeft, liHours;
char szBuffer[20];
FILE *list;
time ( <imeTimeNow );
ldETime = difftime(ltimeTimeNow, mtimeStartTime);
llETime = ldETime; // convert time to integer format (long)
liMinsLeft = llETime / 60; // Calculate complete minutes
liSeconds = llETime - (liMinsLeft * 60); // Calculate seconds
liHours = liMinsLeft / 60; //Calculate Hours
liMinutes = liMinsLeft - (liHours * 60);
list = fopen(_FileName, "a");
fprintf(list, "%02d:%02d:%02d\n", liHours, liMinutes, liSeconds);
fclose(list);
}
Code:
//$Id: ElTimemain.cc
/*
*
* This program is the main program
* It is supposed to accept one parameter and pass it on to the
* Elapsed Time module, where it will use it as the File Name
* to which to output timings.
*
*/
#include <gtkmm/main.h>
#include "ElTimeBody.h"
#include <time.h>
int main (int argc, char *argv[])
{
char szTemp[300];
Gtk::Main kit(argc, argv);
// strcpy(szTemp, &argv[1][0]);
ElapsedTime ElapsedTime;
// ElapsedTime.setFileName(szTemp);
Gtk::Main::run(ElapsedTime); //Shows the window and returns when it is closed.
return 0;
}
Code:
ElTimeBody.h: In member function �void ElapsedTime::setFileName(char)�:
ElTimeBody.h:21: error: invalid conversion from �char� to �const char*�
ElTimeBody.h:21: error: initializing argument 2 of �char* strcpy(char*, const char*)�
ElTimeBody.cc: In member function �virtual void ElapsedTime::on_button_clicked()�:
ElTimeBody.cc:57: warning: converting to �long int� from �double�
ElTimeBody.h: In member function �void ElapsedTime::setFileName(char)�:
ElTimeBody.h:21: error: invalid conversion from �char� to �const char*�
ElTimeBody.h:21: error: initializing argument 2 of �char* strcpy(char*, const char*)�
[
Your error seems to be referring to:
ElTimeBody.h:21: error: invalid conversion from �char� to �const char*�
ElTimeBody.h:21: error: initializing argument 2 of �char* strcpy(char*, const char*)�
I knew it had to be soemthing like that - but there seemed to be about a million combinations, only one right, and I would try the failed ones over again.
If my C wasn't so rusty, I guess I should have been able to work it out,
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.