Thread: [Help] Open files and hotkeys.

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    [Help] Open files and hotkeys.

    Hello, im a NOOB and im trying to learn some C or C++, i want to make a program that open file by pressing a key like this,
    if i press VolumeUp (multimedia keyboard)
    open start.bat
    but im too noob i ve tried with fopen but i dont understand please. thanks.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    By "open a file" you mean ______?

    I'd recommend you start by writing a program that will open a text file and print the contents.

    Post the code you have tried and explain the sense in which it does not work. Remember to use code tags:
    << !! Posting Code? Read this First !! >>
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    First of all, use 'getchar' to get the clicked key

    Now, if the file type is known by the system, use this:
    Code:
    system("Somefile.txt")
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Ok thanks for replying this is the code i tried but it doesnt work

    Code:
    #include <stdio.h>
    int main ()
    {
      FILE *file;
      file = fopen ("C:\Documents and Settings\Administrador\Desktop\Folder\batch.bat","wt");
      
      if (file!=NULL)
      {
        fputs ("Hiiiiiiiiiiiii",file);
        fclose (file);
      }
      return 0;
    }
    i just want to open a file, and the batch will close it few seconds later, i just want the program to open a .bat , and if its possible how to hook it to a multimedia hotkey, i have no idea how to do that im a totally noob at this. Thanks

    Edit :

    Quote Originally Posted by Sipher View Post
    First of all, use 'getchar' to get the clicked key

    Now, if the file type is known by the system, use this:
    Code:
    system("Somefile.txt")
    thanks but i dont even know what include i have to use for "system" function .

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Do you realise that this is a C++ board? Why do u use C style?
    Devoted my life to programming...

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Code:
    #include <cstdio>
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    I said im a totally noob at scripting, so no i didnt realize of nothing lol. thanks im gonna try that

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    C style is fine, altho you should be clear in your head whether you are trying to learn C or C++, there are some differences (at this point, it is not a big deal).

    Anyway, understand there is a difference between opening a file and executing it, which I think you want the later.

    Opening means you are going to examine the contents of the file, eg, as you might view a .bat file in a text editor.

    Executing means you want an executable file (such as a .bat) to be run. For that, you need to use system() or an "exec" style function. Those are the same in C++ as they are in C.

    So find yourself a decent reference and check out "system" first. Here's one:
    http://www.cplusplus.com/reference/c...stdlib/system/
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Note also that your program will have to be running all the time and check if VolumeUp is pressed. There are a lot of ways to do this. What OS are you using?

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Ok i would like to do it in C++, the thing is, what should i write

    Code:
    #include cstudio
    {
    system ("path.extension")
    }
    ?
    thats what im talkin about i dont know how to do it, i know its with the funcion system but what else should i write to make it work,thanks

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Computer language syntax is very exacting. A missing semi-colon can ruin everything.

    Code:
    #include <cstdlib>
    Notice, square brackets.

    Code:
    int main () {
    Every program needs a main(). The return type of main is int!

    Code:
    system("_________");
    You'll have to fill that in.

    Code:
         return 0;
    }
    There's the return value of main.

    There is a complete example at the bottom of that last link I posted for you, BTW. You might want to bookmark the site itself. I have never looked at the tutorials:
    C++ Language Tutorial
    but they do have a lot of good reference material, so probably those are okay too. Eg, check out the one labelled "Structure of a program".

    Not surprisingly, the internet is almost overflowing with resources for programmers of all types.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Ok thanks, i made it work, but , how do i have to write the path exactly it says the file isnt there
    i wrote it like this

    Code:
    system ("C:\Documents and Settings\Administrador\Escritorio\Misc\batch.bat");
    and im gonna try to hook it to the keyboard by doing this

    Code:
    if(GetAsyncKeyState(VK_CONTROL)&&GetAsyncKeyState(VK_MENU)&&GetAsyncKeyState(VK_DELETE));

  13. #13
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    This would get a lot easier if you read one of the tutorials on this site. And, as MK said, there are countless resources online that work wonderful. For not knowing really much about what your doing it's only going to get harder till you learn some of the fundamentals.
    -- Will you show me how to c++?

  14. #14
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by BlueEyed View Post
    Ok thanks, i made it work, but , how do i have to write the path exactly it says the file isnt there
    i wrote it like this

    Code:
    system ("C:\Documents and Settings\Administrador\Escritorio\Misc\batch.bat");
    A backslash in a string or character constant signifies a control character. The compiler will recognise this at compile time and turn the backslash and the character after it into a single corresponding ASCII character (i.e not what you want). To include a backslash in a string or character constant, you must use two in succession. Example: "C:\\Windows\\System32" is a C/C++ representation of the system path (on my system, at least).

Popular pages Recent additions subscribe to a feed

Tags for this Thread