how to import a variable across files

D

dharmesh Gupta

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
 
D

dharmesh Gupta

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_file_name,argv[1]);
if(!in)
{
cout<<"can't open input file.\n";
return 1;
}
while(in)
{
.........processing
}
}

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 said:
dharmesh Gupta said:
i have a program splitted across two files
main_prog.cpp
idr.cpp

my main_prog.cpp accepts a file name a s parameter.

You mean you want your program to recognise command-line arguments?
i want to capture this file name in a variable and use it in idr.cpp

How should i Proceed ?? pls help me

Function 'main' when defined as

int main(int number_of_command_line_args,
char *values_of_command_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
 
B

Buster Copley

dharmesh said:
Thanks for ur reply,

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'.
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


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.
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_file_name,argv[1]);
if(!in)
{
cout<<"can't open input file.\n";
return 1;
}
while(in)
{
.........processing
}
}

and when i use 'orig_file_name' in idr.cpp , the compiler gives
'orig_file_name' undefined.


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 :)
 
J

John Harrison

dharmesh Gupta said:
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"

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

Remove the above two lines.
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_file_name,argv[1]);
if(!in)
{
cout<<"can't open input file.\n";
return 1;
}
while(in)
{
.........processing
}
}

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,

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
 
D

dharmesh Gupta

Many thanks to you.

John Harrison said:
dharmesh Gupta said:
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"

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

Remove the above two lines.
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_file_name,argv[1]);
if(!in)
{
cout<<"can't open input file.\n";
return 1;
}
while(in)
{
.........processing
}
}

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,

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top