how to import a variable across files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dharmesh Gupta

    how to import a variable across files

    Hi,
    i have a program splitted across two files
    main_prog.cpp
    idr.cpp

    my main_prog.cpp accepts a file name a s parameter.
    i want to capture this file name in a variable and use it in idr.cpp

    How should i Proceed ?? pls help me

    Thanks
    Dharmesh
  • dharmesh Gupta

    #2
    Re: how to import a variable across files

    Thanks for ur reply,
    but it wasn't exactly i wanted, let me be more specific on this
    i give the code here, code goes like this

    #include "idr.cpp"
    #include <string>
    #include <iostream>
    #include <fstream>
    #include <new>
    using namespace std;
    //extern char* orig_file_name;
    extern char orig_file_name[40];
    int main(int argc, char *argv[])
    {
    char** record ; //Declare pointer to a pointer, or in this case
    a pointer to an array of pointers
    record = new char*[26]; //Pointers to 26 arrays
    for(int c=0; c<26; c++)
    { record[c] = new char[24];} //Creates an array for each pointer in
    x array
    char *str;
    str= new char[250];
    char *str1;
    str1 = new char[50];
    if(argc!=2)
    {
    cout<<"usage:";
    cout<<argv[0];
    return 1;
    }
    ifstream in(argv[1]);
    strcpy(orig_fil e_name,argv[1]);
    if(!in)
    {
    cout<<"can't open input file.\n";
    return 1;
    }
    while(in)
    {
    .........proces sing
    }
    }

    and when i use 'orig_file_name ' in idr.cpp , the compiler gives
    'orig_file_name ' undefined.
    I now hope that the problem is more clear.

    thanks,



    "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message news:<vhfu7d3e6 d5g7c@corp.supe rnews.com>...[color=blue]
    > "dharmesh Gupta" <dharmesh.gupta @esteltelecom.c om> wrote...[color=green]
    > > i have a program splitted across two files
    > > main_prog.cpp
    > > idr.cpp
    > >
    > > my main_prog.cpp accepts a file name a s parameter.[/color]
    >
    > You mean you want your program to recognise command-line arguments?
    >[color=green]
    > > i want to capture this file name in a variable and use it in idr.cpp
    > >
    > > How should i Proceed ?? pls help me[/color]
    >
    > Function 'main' when defined as
    >
    > int main(int number_of_comma nd_line_args,
    > char *values_of_comm and_line_args[])
    >
    > gives the program the ability to see what the hosting environment
    > (operating system, for example) passes to it when the program is
    > executing. Here is an example:
    >
    > #include <iostream>
    > using namespace std;
    > int main(int argc, char *argv[])
    > {
    > cout << "Got " << argc << " command line arguments\n";
    > cout << "They are:\n";
    > for (int arg = 0; arg < argc; ++arg)
    > cout << "[" << arg << "] \"" << argv[arg] << "\"\n";
    > cout << endl;
    > }
    >
    > You can extract the value of any command-line argument and then
    > use it (convert it, pass it around, etc.)
    >
    > Victor[/color]

    Comment

    • Buster Copley

      #3
      Re: how to import a variable across files

      dharmesh Gupta wrote:[color=blue]
      > Thanks for ur reply,[/color]

      Rather than top-posting you should organise your replies inline, to make
      it easy to see what you are replying to without reading the entire
      thread. And this isn't a txt msg; the correct spelling is 'your'.
      [color=blue]
      > but it wasn't exactly i wanted, let me be more specific on this
      > i give the code here, code goes like this
      >
      > #include "idr.cpp"
      > #include <string>
      > #include <iostream>
      > #include <fstream>
      > #include <new>
      > using namespace std;
      > //extern char* orig_file_name;
      > extern char orig_file_name[40];
      > int main(int argc, char *argv[])
      > {
      > char** record ; //Declare pointer to a pointer, or in[/color]
      this case[color=blue]
      > a pointer to an array of pointers[/color]


      Be careful what you say. A pointer to an array of pointers is declared
      char * (* record) [N];
      for N a compile-time constant integer, but that's not what you mean.
      [color=blue]
      > record = new char*[26]; //Pointers to 26 arrays
      > for(int c=0; c<26; c++)
      > { record[c] = new char[24];} //Creates an array for each pointer in
      > x array
      > char *str;
      > str= new char[250];
      > char *str1;
      > str1 = new char[50];
      > if(argc!=2)
      > {
      > cout<<"usage:";
      > cout<<argv[0];
      > return 1;
      > }
      > ifstream in(argv[1]);
      > strcpy(orig_fil e_name,argv[1]);
      > if(!in)
      > {
      > cout<<"can't open input file.\n";
      > return 1;
      > }
      > while(in)
      > {
      > .........proces sing
      > }
      > }
      >
      > and when i use 'orig_file_name ' in idr.cpp , the compiler gives
      > 'orig_file_name ' undefined.[/color]


      The error says it all. `orig_file_name ' is not defined, because an
      'extern' declaration is not a definition. Replace that line with a
      definition:
      char orig_file_name [40];
      and move the definition _above_ the #include "idr.cpp" directive.

      However, you need to get your head around separate compilation. What I
      think you want to do is
      - Remove the #include "idr.cpp" directive.
      - Put an 'extern' declaration of orig_file_name in a header file.
      - Include the header in both main.cpp and idr.cpp.
      - Put a definition of orig_file_name in exactly one of the two
      translation units (or on its own in a third, which should also include
      your header).
      - Link the two translation units together after compilation (with my
      setup, "g++ main.cpp idr.cpp" would be one way to do it).

      Instead of using a global variable like this, another possibility is to
      pass the file name as an argument to whatever function in idr.cpp uses
      it. Global variables should not be used unless it is necessary.

      Quite aside from that, you should consider using the standard C++ string
      class instead of messing around with char *. Your program as it stands
      is a recipe for disaster.

      [snip original post]

      Regards,
      Buster.

      P.S.

      Dharmesh, apologies for replying in an email. I only intended to reply
      to the group. I'll get it right next time, just watch :)

      Comment

      • John Harrison

        #4
        Re: how to import a variable across files


        "dharmesh Gupta" <dharmesh.gupta @esteltelecom.c om> wrote in message
        news:457ed71a.0 307182121.77421 4a4@posting.goo gle.com...[color=blue]
        > Thanks for ur reply,
        > but it wasn't exactly i wanted, let me be more specific on this
        > i give the code here, code goes like this
        >
        > #include "idr.cpp"[/color]

        This is wrong, never include one cpp file in another cpp file.
        [color=blue]
        > #include <string>
        > #include <iostream>
        > #include <fstream>
        > #include <new>
        > using namespace std;
        > //extern char* orig_file_name;
        > extern char orig_file_name[40];[/color]

        Remove the above two lines.
        [color=blue]
        > int main(int argc, char *argv[])
        > {
        > char** record ; //Declare pointer to a pointer, or in this case
        > a pointer to an array of pointers
        > record = new char*[26]; //Pointers to 26 arrays
        > for(int c=0; c<26; c++)
        > { record[c] = new char[24];} //Creates an array for each pointer in
        > x array
        > char *str;
        > str= new char[250];
        > char *str1;
        > str1 = new char[50];
        > if(argc!=2)
        > {
        > cout<<"usage:";
        > cout<<argv[0];
        > return 1;
        > }
        > ifstream in(argv[1]);
        > strcpy(orig_fil e_name,argv[1]);
        > if(!in)
        > {
        > cout<<"can't open input file.\n";
        > return 1;
        > }
        > while(in)
        > {
        > .........proces sing
        > }
        > }
        >
        > and when i use 'orig_file_name ' in idr.cpp , the compiler gives
        > 'orig_file_name ' undefined.
        > I now hope that the problem is more clear.
        >
        > thanks,
        >[/color]

        Here's how you do it. Create a third file, called main_prog.h (say)

        #ifndef MAIN_PROG_H
        #define MAIN_PROG_H

        extern char orig_file_name[];

        #endif

        Now write main_prog.cpp like this

        #include "main_prog. h"
        ....
        char orig_file_name[40];
        ....

        and idr.cpp like this

        #include "main_prog. h"
        ....

        Make sure you compile and link main_prog.cpp and idr.cpp (how you do this
        obviously depends on what compiler you are using).

        Header files are for what you want to share between cpp files, which should
        be compiled seperately. Since you want to share orig_file_name between
        main_prog.cpp and idr.cpp its declaration should go in a header file, which
        you can then include in both cpp files.

        john


        Comment

        • dharmesh Gupta

          #5
          Re: how to import a variable across files

          Many thanks to you.

          "John Harrison" <john_andronicu s@hotmail.com> wrote in message news:<bfaocc$cs 1jt$1@ID-196037.news.uni-berlin.de>...[color=blue]
          > "dharmesh Gupta" <dharmesh.gupta @esteltelecom.c om> wrote in message
          > news:457ed71a.0 307182121.77421 4a4@posting.goo gle.com...[color=green]
          > > Thanks for ur reply,
          > > but it wasn't exactly i wanted, let me be more specific on this
          > > i give the code here, code goes like this
          > >
          > > #include "idr.cpp"[/color]
          >
          > This is wrong, never include one cpp file in another cpp file.
          >[color=green]
          > > #include <string>
          > > #include <iostream>
          > > #include <fstream>
          > > #include <new>
          > > using namespace std;
          > > //extern char* orig_file_name;
          > > extern char orig_file_name[40];[/color]
          >
          > Remove the above two lines.
          >[color=green]
          > > int main(int argc, char *argv[])
          > > {
          > > char** record ; //Declare pointer to a pointer, or in this case
          > > a pointer to an array of pointers
          > > record = new char*[26]; //Pointers to 26 arrays
          > > for(int c=0; c<26; c++)
          > > { record[c] = new char[24];} //Creates an array for each pointer in
          > > x array
          > > char *str;
          > > str= new char[250];
          > > char *str1;
          > > str1 = new char[50];
          > > if(argc!=2)
          > > {
          > > cout<<"usage:";
          > > cout<<argv[0];
          > > return 1;
          > > }
          > > ifstream in(argv[1]);
          > > strcpy(orig_fil e_name,argv[1]);
          > > if(!in)
          > > {
          > > cout<<"can't open input file.\n";
          > > return 1;
          > > }
          > > while(in)
          > > {
          > > .........proces sing
          > > }
          > > }
          > >
          > > and when i use 'orig_file_name ' in idr.cpp , the compiler gives
          > > 'orig_file_name ' undefined.
          > > I now hope that the problem is more clear.
          > >
          > > thanks,
          > >[/color]
          >
          > Here's how you do it. Create a third file, called main_prog.h (say)
          >
          > #ifndef MAIN_PROG_H
          > #define MAIN_PROG_H
          >
          > extern char orig_file_name[];
          >
          > #endif
          >
          > Now write main_prog.cpp like this
          >
          > #include "main_prog. h"
          > ...
          > char orig_file_name[40];
          > ...
          >
          > and idr.cpp like this
          >
          > #include "main_prog. h"
          > ...
          >
          > Make sure you compile and link main_prog.cpp and idr.cpp (how you do this
          > obviously depends on what compiler you are using).
          >
          > Header files are for what you want to share between cpp files, which should
          > be compiled seperately. Since you want to share orig_file_name between
          > main_prog.cpp and idr.cpp its declaration should go in a header file, which
          > you can then include in both cpp files.
          >
          > john[/color]

          Comment

          Working...