Thread: Opening a file from the a console program

  1. #1
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46

    Question Opening a file from the a console program

    Is it possible? If so someone please tell me how to open a file... eg. i want to the user to view a shockwave movie or i want to open word thru c++(console) ...
    Plz someonle help me out...
    Im a newbie..

    Pritin

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115
    Do you mean executing an executable file (.exe) or you mean how to open and read file with ASCII content (.txt for example) ?
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  3. #3
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    From the mention of shockwave movie and all that, I'd guess running an executable.

    Umm, there's system(), but everyone hates using that.


    Umm, here's some stuff from MSDN, http://msdn.microsoft.com/library/de...e_handling.asp, specifically you'd want _execl to wexecvpe, then all the _spawn ones

    Then you can pass stuff like the file to open via command line arguments for a bunch of programs. Passing IE a website as a param makes it open up and go to that website for example.

  4. #4
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46
    Sorry i forgot to mention i was using Borland c++ Builder...

    The site you provided me above were related to Visual C++...
    Does it make a difference? or are the commands the same...

    If the commands are same, could someone please please post a sample snippet?

    Thanx a lot for helping though..
    ..
    ..
    Pritin

    P.S :: I did not mean files with ASCII value, i meant executables...

    I just started C++... hardly a month...

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115
    ShellExecute is also a good way....
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  6. #6
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46
    Sorry to be such a pain in the neck, but isnt the previous post by "publikum" a Windows application? Or is it for the console and has a particular header file...

    If im wrong please correct me...

    Just in case my query still isnt clear,...
    ------------
    Say for example im making a tutorial comprising of several separate shockwave or flash movies... I want to let the user open and view the movies, and when the user quits, i want to have stored the last movie viewed, and continue from there next time.
    Please note :: Ive just started C++

    -------------


    Thanx people,

    Pritin.
    [email protected]

  7. #7
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    If im wrong please correct me...
    At the bottom of the page it tells you what header to use and what library to import. You can use ShellExecute in a console program, it's not restricted to Win32 applications.
    Just because I don't care doesn't mean I don't understand.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    EDIT: Minute late.

    I believe (from the content of the afore-linked webpage) that ShellExecute is a premade function with the header file "shellapi.h" Info on this function (well, other than syntax details and use) is at the bottom of the link publikum mentioned.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This is in the FAQ.
    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.

  10. #10
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46
    My compiler does not have shellapi.h -- it's turbo c++ ... very primitive... is there any other way..?

    Could someone tell me how exactly it can be done using system("...")?

    I saw it in FAQ, but it dint really work... it just took a lot of time compiling... screen turned black...(on execution) and returned back to normal... no exe was executed...

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    its a better idea to get a new compiler. try dev-c++
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46

    Question Problem with Dev c++ 4.9.9.2

    The reason i try to avoid using dev c++ even though i have it, is because it gives lots of erreors... so i only use it to print the prog.

    eg.

    Code:
    #include<iostream.h>
    
    void main()
    {
    cout << "Hello";
    }
    Gives me so many errors :: Something about using an inappropriate header file... void must return int.. and so on...

    Someone plz help...

  13. #13
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Gives me so many errors :: Something about using an inappropriate header file... void must return int.. and so on...
    You should get used to this kind of error-messages. In the near future you won't find any compiler that compiles this kind of wrong code.
    Kurt

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    Well, in C++, headers only contain .h if they are (taking from someone else's post from who I forgot) non-standard or part of the original C library, and if they're part of the C library you add a "c" the beginning of the header file's name. stdio.h would become cstdio. iostream is part of the standard C++ library, and so it just "iostream". Dev-C++ calls files with .h "antiquitated" or "decaprecated". And errors that are fixed both make the program better and give you more knowledge about C++ in how code should be built.

    EDIT: And yeah, main MUST return an int. When the program exits, main returns a value automatically to the system. Saying main is void violates the returning of the int, resulting in the error, resulting on more knowledge of how code is built.
    It's Link, not linkofazeroth. The latter's just my username. The former's my online identity. Thank you.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    So, instead of this code
    Code:
    #include<iostream.h>
    
    void main()
    {
    cout << "Hello";
    }
    you should try
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
        cout << "Hello, World!\n";
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Opening a .html file from the console
    By Verdagon in forum C++ Programming
    Replies: 9
    Last Post: 08-02-2005, 05:05 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Opening a file in a different program...
    By gcn_zelda in forum Windows Programming
    Replies: 2
    Last Post: 09-07-2003, 10:44 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM