Wednesday, April 6, 2011

Opening a file through argv[1] C++ Question

Hi,

I have check to see if the argument in argv[1] is valid for my file.

i have a menu system in its own sub-routine called in main.

Reading the file and so on is done in another sub-routine sepearte from the first, also called in main.

How can i transfer the argv[1] to the first sub-routine? Or even the second?

string sArgInit = argv[1]

This way i can open the file using a C-String.

But i cant get the string to any function outside of main..

is there a way to do this without: global varaibles, passing the string as an argument paramter to the sub-routine.

From stackoverflow
  • const char *filename = argv[1];
    

    Then pass the pointer to your subroutine. argv[1] remains valid for the whole execution of the program.

    Brian Neal : This doesn't seem like an unreasonable answer given the question's vagueness.
  • First of all, you may want to check that argv[1] is not null before you try to assign it to the C++ string.

    You can move the C++ string object around to functions.

    Eventually, you can use c_str() to make a C string

    For example: char* sData = (char*)str.c_str()

    Martin York : c_str() returns a char const* why do you want to cast away the constness?
    Uri : I was trying to illustrate that he can just put it as the parameter to fopen or whatever. Besides, I copy pasted that line from somewhere else :)
  • The following code shows how to do exactly what you want, checking that argv[1] exists before passing it to functions as C char-pointers or C++ strings and using the value within that function.

    using namespace std;
    #include <iostream>
    #include <string>
    
    static void f1 (char *s) {
        cout << "1: " << s << endl;
    }
    
    static void f2 (string *s) {
        cout << "2: " << (*s) << endl;
        cout << "3: " << s->c_str() << endl;
    }
    
    int main (int argc, char *argv[]) {
        if (argc < 2) {
            cout << "Usage: " << argv[0] << " <test_string>" << endl;
        }
    
        char *s1 = argv[1];
        string *s2 = new string (argv[1]);
    
        f1 (s1);
        f2 (s2);
    
        return 0;
    }
    

    The output is, as expected:

    1: hello
    2: hello
    3: hello
    

    As for your edit, you cannot access argc/argv without storing them in a global or passing them to a function. That's because they're passed to the main function as arguments so inherently local to that function.

    Brian Neal : Why would you new up a string here? You are leaking memory.
    Martin York : s2 leaks. Why are you newing it?
    paxdiablo : For a start, the new is irrelevant to the Q/A, it was just put there to show the different ways to pass a 'string'. And there's no memory leak, all memory is recovered when the process exits. Do you both think that new'ed variables continue to exist after the process is finished? No.
  • is there a way to do this without: global varaibles, passing the string as an argument paramter to the sub-routine.

    No, you have to get it to the routine somehow.

    What is wrong with passing it as a parameter?

    void my_sub( char* s){}
    
    void main(int argc, char* argv[])
    {
        my_sub( argv[1] );
    }
    
  • You would have to pass the argument as a parameter to the sub-routines.

    void method1(char* filename) {
      // ..
    }
    
    void method2(char* filename) {
      // ...
    }
    
    void main(int argc, char* argv[]) {
      method1(argv[1]);
      method2(argv[1]);
    }
    

    I'm not sure if this answers your question, so please expand.

  • class myprog {
    public:
        myprog(const string& filename) : m_filename(filename) {}
    
        void menufunc();
        void otherfunc();
    private:
        string m_filename;
    };
    
    int main(int argc, char** argv)
    {
        myprog prog(argv[1]);
    
        prog.menufunc();
        prog.otherfunc();
        return 0;
    }
    

0 comments:

Post a Comment