write to txt file

This is a discussion on write to txt file within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to write info to a txt file. All I am getting in the txt file is ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    write to txt file

    Hi,

    I am trying to write info to a txt file.

    All I am getting in the txt file is message.

    Code:
    // include files nedeed by our code.
    #include <plugin.h>
    #include <stdio.h>
    #include <lib/gui/ewindow.h>
    #include <lib/gui/elabel.h>
    #include <lib/gui/ebutton.h>
    #include <lib/gui/emessage.h>
    #include <lib/gui/textinput.h>
    
    
    // The Class declaration of our Main Window
    class eBibleMainWindow: public eWindow
    {
    	// the label to show the text
    	eLabel *label;
        // the textinput
    	eTextInputField *mytext;
    	eTextInputField *mytext1;
    	eTextInputField *mytext2;
    	eTextInputField *mytext3;
    	eTextInputField *mytext4;
    	eTextInputField *mytext5;
    	eTextInputField *mytext6;
    	eTextInputField *mytext7;
    	// function to execute when button is pushed
    	void message1();
    	
    public:
    		// the constructor.
    	eBibleMainWindow();
    		// the destructor.
    	~eBibleMainWindow();
    };
    
    
    // The plugin entry point, Here start the code execution
    extern "C" int plugin_exec( PluginParam *par )
    {
    	// our demo dialog instance.
    	eBibleMainWindow dlg;
    		// show the dialog...
    	dlg.show();
    		// give control to dialog.. (the dialog is modal!)
    	int result=dlg.exec();
    		// and after it, hide it again.
    	dlg.hide();
    	return result;
    }
    
    
    eBibleMainWindow::eBibleMainWindow(): eWindow(1)
    {
    		// move our dialog to 100.100...
    	cmove(ePoint(100, 100));
    		// ...and give x and y dimensions.
    	cresize(eSize(550, 376));
    		// set a title.
    	setText("!!EDITOR!!");
    
    	// create a label to show a text.
    	label=new eLabel(this);
    	// give a position
    	label->move(ePoint(20, 50));
    	// set the label dimensions
    	label->resize(eSize(400, 100));
    	// set the label text
    	label->setText("Enter Here.");
    
    	// create textinput
    	mytext=new eTextInputField(this);
    	// give position
    	mytext->move(ePoint(20, 100));
    	// give size
    	mytext->resize(eSize(25, 40));
    	// set max number of characters
    	mytext->setMaxChars(1);
    	//mytext->setUseableChars("1234567890");
    	//mytext->setText(codeentry);
    	// show a frame decoration
    	mytext->loadDeco();
    
    	// create buttons and set properties
    	eButton * ok = new eButton(this);
    	ok->setText("Update Key File");
    	ok->move(ePoint((clientrect.width() - 60)/4, clientrect.height() - 60));
    	ok->resize(eSize(300, 40));
    	ok->setShortcut("green");
    	ok->setShortcutPixmap("green");
    	ok->loadDeco();
    	// function to call when button is pushed
        CONNECT(ok->selected, eBibleMainWindow::message1);
    
    	//set focus to textinput
    	setFocus(mytext);
    
    }
    
    void eBibleMainWindow::message1()
    {
    	// declare variable we will use in this function
    	eString message, message2;
    	// assign to message2 the textinput content
    		message2 = mytext->getText();
    	
    	// compose message concatenating strings
    	message = message2;
    	
    // create messagebox (two buttons yes/no)
        eMessageBox box((message), "Question", eMessageBox::btYes|eMessageBox::btNo|eMessageBox::iconQuestion, eMessageBox::btYes);
        // show it
        box.show();
        // execute code and store in the variable button the result (button pressed)
        int button = box.exec();
        // hide it after execution
        box.hide();
        // exit if button yes was pushed.
        if (button == eMessageBox::btYes)
        {
              FILE *fp;
    		  fp = fopen ("/var/keys/1.txt", "w");
    		  if (fp)
    		    {
    		      fprintf (fp, "message");
    		      fclose (fp);
        }
        }
    
    }
    
    eBibleMainWindow::~eBibleMainWindow()
    {
    	// we have to do almost nothing here. all widgets are automatically removed
    	// since they are child of us. the eWidget-destructor will to this for us.
    }

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Because you have put this in:

    fprintf (fp, "message");
    =========================================
    Everytime you segfault, you murder some part of the world

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    This is also not C.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    How do I get it to write what message is equal to.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Moved to C++ programming forum.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Well I don't know what 'eString' exactly is, but I'm guessing it's a string, also I don't know C++

    But, this is how we fprintf an integer in an array:
    Code:
    fprintf(fw,"&#37;d ",arr[k]);
    So, I'm guessing for a string it's

    Code:
    fprintf(fw,"%s",message);
    Something like that

    I don't do C++, so I don't know the C++ syntax

    In your case it will be fp not fw
    =========================================
    Everytime you segfault, you murder some part of the world

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Quote Originally Posted by JFonseka View Post
    Well I don't know what 'eString' exactly is, but I'm guessing it's a string, also I don't know C++

    But, this is how we fprintf an integer in an array:
    Code:
    fprintf(fw,"%d ",arr[k]);
    So, I'm guessing for a string it's

    Code:
    fprintf(fw,"%s",message);
    Something like that

    I don't do C++, so I don't know the C++ syntax

    In your case it will be fp not fw
    Tried that, but on compile, I got:

    bibledemo.cpp: In member function `void eBibleMainWindow::message1()':
    bibledemo.cpp:249: warning: cannot pass objects of non-POD type `class eString' through `...'; call will abort at runtime

    Thanks for trying to help.

  8. #8
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Perhaps it's something to do with the &#37;s

    Can't help any further, I don't really recognize those errors.

    Cheers
    =========================================
    Everytime you segfault, you murder some part of the world

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you need some sort of translation from eString into a text-string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21