LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-26-2005, 04:50 PM   #1
Ian D
Member
 
Registered: Aug 2005
Location: Solihull, UK
Distribution: Fedora 39
Posts: 215

Rep: Reputation: 18
gtk - How to pass in a parameter


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.

TIA

Ian
 
Old 10-26-2005, 04:58 PM   #2
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
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.
 
Old 10-27-2005, 02:22 PM   #3
Ian D
Member
 
Registered: Aug 2005
Location: Solihull, UK
Distribution: Fedora 39
Posts: 215

Original Poster
Rep: Reputation: 18
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 ( &ltimeTimeNow );
  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*)�
[
 
Old 10-28-2005, 10:16 AM   #4
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
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*)�

so change:
Code:
    void setFileName(char FileName) {strcpy(_FileName, FileName);}
to
Code:
    void setFileName(const char *FileName) {strcpy(_FileName, FileName);}
 
Old 10-28-2005, 11:23 AM   #5
Ian D
Member
 
Registered: Aug 2005
Location: Solihull, UK
Distribution: Fedora 39
Posts: 215

Original Poster
Rep: Reputation: 18
Thanks,

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,

Anyway, my program just sings now.

Ian
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to change and pass thread parameter back to main dragondad Programming 3 09-19-2005 01:58 PM
how do I pass a module parameter? maerong Debian 2 02-07-2005 07:33 AM
pass a parameter to a running program nimra Linux - Software 1 06-29-2004 01:38 AM
bash: pass a parameter to xview nimra Programming 2 06-28-2004 09:18 PM
PHP Pass Parameter Gerardoj Programming 2 05-25-2004 08:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration