通过argv [1] C ++打开文件

时间:2021-02-16 01:19:36

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

我检查了argv [1]中的参数是否对我的文件有效。

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

我在自己的子程序中有一个菜单系统,名为main。

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

读取文件等是在与第一个分开的另一个子例程中完成的,也在main中调用。

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

如何将argv [1]转移到第一个子程序?甚至第二个?

string sArgInit = argv[1]

string sArgInit = argv [1]

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

这样我就可以使用C-String打开文件。

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

但是我不能把字符串带到main之外的任何函数..

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

有没有办法在没有:全局变量的情况下执行此操作,将字符串作为参数传递给子例程。

6 个解决方案

#1


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.

下面的代码显示了如何正确地执行所需操作,检查argv [1]是否存在,然后将其作为C char-pointers或C ++字符串传递给函数并使用该函数中的值。

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

static void f1 (char *s) {
    cout << "1: " << s << endl;
}

static void f2 (const 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;
        return EXIT_FAILURE;
    }

    char *s1 = argv[1];
    string s2(argv[1]);

    f1 (s1);
    f2 (s2);

    return EXIT_SUCCESS;
}

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.

至于编辑,如果不将argc / argv存储在全局或将它们传递给函数,则无法访问它。那是因为它们作为参数传递给main函数,因此该函数本身就是本地的。

#2


is there a way to do this without: global variables, passing the string as an argument parameter 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] );
}

#3


const char *filename = argv[1];

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

然后将指针传递给子程序。 argv [1]对整个程序的执行仍然有效。

#4


First of all, you may want to check that argv[1] is not null before you try to assign it to the C++ string.

首先,在尝试将其分配给C ++字符串之前,您可能需要检查argv [1]是否为null。

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

您可以将C ++字符串对象移动到函数中。

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

最终,您可以使用c_str()来创建C字符串

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

例如:char * sData =(char *)str.c_str()

#5


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.

我不确定这是否能回答你的问题,所以请扩展。

#6


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;
}

#1


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.

下面的代码显示了如何正确地执行所需操作,检查argv [1]是否存在,然后将其作为C char-pointers或C ++字符串传递给函数并使用该函数中的值。

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

static void f1 (char *s) {
    cout << "1: " << s << endl;
}

static void f2 (const 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;
        return EXIT_FAILURE;
    }

    char *s1 = argv[1];
    string s2(argv[1]);

    f1 (s1);
    f2 (s2);

    return EXIT_SUCCESS;
}

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.

至于编辑,如果不将argc / argv存储在全局或将它们传递给函数,则无法访问它。那是因为它们作为参数传递给main函数,因此该函数本身就是本地的。

#2


is there a way to do this without: global variables, passing the string as an argument parameter 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] );
}

#3


const char *filename = argv[1];

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

然后将指针传递给子程序。 argv [1]对整个程序的执行仍然有效。

#4


First of all, you may want to check that argv[1] is not null before you try to assign it to the C++ string.

首先,在尝试将其分配给C ++字符串之前,您可能需要检查argv [1]是否为null。

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

您可以将C ++字符串对象移动到函数中。

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

最终,您可以使用c_str()来创建C字符串

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

例如:char * sData =(char *)str.c_str()

#5


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.

我不确定这是否能回答你的问题,所以请扩展。

#6


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;
}