Thread: How do you assign a "Button" a task ?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    How do you assign a "Button" a task ?

    Hi

    I am completely new to C++ though I have knowledge in more simplier codinglanguages.
    Now I am trying to locate where and a bit how I will write code.

    I have made a "New Project" (Window Forms Application) and I have put a "Button" to my Form.

    Now is my question this. How would the code look like to open this textfile(C:\test.txt) when I press the button.
    And very important for me to know is also where I will put this code.
    For the form I have a lot of code and for the "Button1" the code looks as follows.

    Thank You...



    //
    // button1
    //
    resources->ApplyResources(this->button1, L"button1");
    this->button1->ForeColor = System:rawing::SystemColors::AppWorkspace;
    this->button1->Name = L"button1";
    this->button1->UseVisualStyleBackColor = true;
    this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you're new to C++, you probably don't want to be creating Windows applications right off the bat. Start with simpler console applications.

    As for reading files: http://www.cprogramming.com/tutorial/lesson10.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Eww, this looks like .NET.... managed C++.....bad stuff......

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I recommend you to really use C# if you want to use CLR/.NET.
    C++ CLI is a Microsoft-only, vendor lock-in.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    Smile Thanks

    Thanks for the great help. I will try the link about "opening files" to make a console program.
    I am not new to programming and have done some simplier consoleprograms.
    But I have to understand the use of libriaries, classes and headerfiler wich I am not really familiar of yet how to use exactly.
    I will try some code out in the consoleprogram

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then you could probably start off with some easier native C++ with consoles and do exercises. As noted, Windows programming is difficult and should be for the experienced.
    Anyway, good luck.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I will try to make a consoleprogram that opens an existing file on the location (C:\Test.txt).
    I have made a "New Project" in C++ wich is named Testar.cpp

    I would be very happy to have some assistance for how the code will look like.
    I received som examplecode below but what I should need is a finished code that I can insert and study and understand the logic of why it works.

    Thank You...

    This was the examplecode:
    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char str[10];
    
      //Creates an instance of ofstream, and opens example.txt
      ofstream a_file ( "example.txt" );
      // Outputs to example.txt through a_file
      a_file<<"This text will now be inside of example.txt";
      // Close the file stream explicitly
      a_file.close();
      //Opens for reading the file
      ifstream b_file ( "example.txt" );
      //Reads one string from the file
      b_file>> str;
      //Should output 'this'
      cout<< str <<"\n";
      cin.get();    // wait for a keypress
      // b_file is closed implicitly here
    }

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Instead of trying to have us explain every single line, why not do us the courtesy of narrowing down what you want us to explain? I mean, after all, there are comments in the code explaining it....

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What are you looking for?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes I will try to explain exactly what I will try to do. As I am new to C++ but not programming. I would need to see how how a finished code could look like for example this:

    I have a file named and located: (C:\test.txt)

    Now I will explain how the consoleprogram will work in practic:
    - The program will ask me this: "What file would you like to open ?"
    and I will answer: test.txt

    The file will open when I write this and press Enter. If the file is not found the program will tell me: "The file doesn&#180;t exist" (I suppose this is an else statement for this)

    I know << cout and >> cin is used for what I am experiment to do but will be greatful for how the whole program look like with all the small details to understand the logic.
    I think it would be nessecary for me. Hope my desription helps.

    Thanks...
    Last edited by Coding; 12-16-2007 at 01:59 PM.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes, you should be able to make that using an *fstream, and of course, cout, and cin, but I don't understand what you want to do when you open the file. If it's either simple reading or simple writing, you should be able to modify the example you posted, or find a similar example that does something closer to what you want.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int main()
    {
    	cout << "What file would you like to open?\n";
    	string file;
    	getline(cin, file);
    	istream iFile(file.c_str());
    	if (!iFile.is_open())
    	{
    		cout << "Error: File doesn't exist.\n";
    		return 1;
    	}
    	// Do some reading perhaps?
    	string line;
    	getline(iFile, line);
    	cout << "First line in file:\n" << line << endl;
    	iFile.close(); // Not necessary, but here goes anyway.
    	return 0;
    }
    Basic example. Not tested.
    Last edited by Elysia; 12-16-2007 at 02:18 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Elysia

    Thank You... I am trying to put your code into "Visual C++ 2008 Express Edition" and then I suppose I will go to the menu- Build and Press "Build Testar" as my project is named "Testar".
    I do attach in the beginning of the code, before int main():
    #include <fstream>

    When I do this I get the line:
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


    What could be wrong ?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Next time, you could specify what compile errors you get, since that's really unhelpful.
    Of course, it seems the example is flawed and not working, but that's probably because I never use C++ I/O.
    Someone else will have to correct and rewrite that code. I don't know how to use them properly and the doc is really of no help.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I understand. For me it is very difficult as I am new to this: The error report look like this:
    Code:
    .\Testar.cpp(1) : warning C4627: '#include <fstream>': skipped when looking for precompiled header use
            Add directive to 'stdafx.h' or rebuild precompiled header
    .\Testar.cpp(22) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
    Build log was saved at "file://c:\Documents and Settings\Anderas\My Documents\Visual Studio 2008\Projects\Testar\Testar\Debug\BuildLog.htm"
    I have tried to put: #include "stdafx.h" in the beginning too but doesn&#180;t work either.
    But I am very happy for the help I got anyway..
    Thank You...
    Last edited by Coding; 12-16-2007 at 02:38 PM.

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, 09:35 AM
  2. Where do a task get "wakeuped", blocked, preempted?
    By micke_b in forum Linux Programming
    Replies: 4
    Last Post: 12-28-2007, 04:49 PM
  3. A Task Buffer for storing socket descriptors
    By cloudy in forum Networking/Device Communication
    Replies: 0
    Last Post: 09-09-2006, 01:08 PM
  4. Linked list question....again
    By dP munky in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2003, 05:59 PM
  5. Scheduling Algo
    By BigDaddyDrew in forum C++ Programming
    Replies: 41
    Last Post: 03-08-2003, 11:00 AM