Thread: Saving and Loading Files

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    18

    Saving and Loading Files

    Hi,

    In my graphics application, I have a class that deals with saving and loading files and I've created my own extension .x . I have a method in that class that takes the name of a file, opens it, reads it and generates a particular scene according to the syntax I've defined previously. But the thing is: I don't want to change my code every time I want to open a different file!

    Here's what I have already done: I have associated my new .x extension to the executable in question. What I want to know is how to pass my file's name to my executable the minute I doubleclick it.

    I have a feeling it has something to do with the arguments in
    Code:
    int main(int argc, char **argv)
    Thanks for your help.

    renanmzmendes

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Using int main parameters are only going to get you as far as being able to say what file you want as part of your shortcut. The best way, is either use a dialog from what ever GUI library you are using and ask on startup, or compile with the command window still there, and ask there.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    Hi, Wraithan.

    I'm not quite sure if I completely understood what you've said, but I didn't want to have to type my file name every time I used my application. I wan't to use it like you'd use MSWord, for instance. When you doubleclick your file, the Operational System selects that application to be executed, and in addition makes it read and generate that file.
    It would solve my problem if I could pass my file name as the argument **argv in main, when I doubleclicked the file. With this at hand, I could easily open, read and generate it using my application...

    I hope I was clear enough.

    renanmzmendes

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Easy.
    (Assuming you are using Windows) Double-click the file with filename X. Select application to open. Tick "Always use this program", select your app, and OK.
    Done.
    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
    Jan 2008
    Posts
    18
    Yeah, that's what I have already done (according to my first message). The thing is: in my code I need to know the filename to be able to use:

    Code:
    ifstream reader;
    
    reader.open("filename.x");
    With what you just told me, I would be only running my app, without using my filename, thus not opening it. I need to know how to pass my filename as an argument of my main when I doubleclick it, just like in any other respectable app...

    Thanks

    renanmzmendes

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    argc -> Number of arguments passed to your app (including the filename of your exe).
    argv -> A 2D array of arguments passed to your app.
    argv[0] -> Array of the filename of your program.
    argv[1] -> First argument passed to your exe.
    argv[2] -> Second argument passed to your exe.
    ...
    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
    Jan 2008
    Posts
    18
    Thanks, Elysia.

    I found out that argv[1] will be the filename when you doubleclick the file with the extension associated to your executable. That shall do it!

    renanmzmendes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read files?
    By Raigne in forum C++ Programming
    Replies: 17
    Last Post: 11-23-2005, 05:03 PM
  2. Data Saving and Loading
    By 6arredja in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2005, 09:35 AM
  3. Major Problems with GetSaveFileName() and GetOpenFileName()
    By CodeHacker in forum Windows Programming
    Replies: 8
    Last Post: 07-12-2004, 11:05 AM
  4. Saving and Loading Linked Lists With File i/o
    By Red Army in forum C++ Programming
    Replies: 8
    Last Post: 05-15-2002, 03:19 PM