473,804 Members | 3,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 19 '05 #1
4 8180
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.********@att Abi.com> wrote in message news:<vh******* *****@corp.supe rnews.com>...
"dharmesh Gupta" <dh************ @esteltelecom.c om> wrote...
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_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

Jul 19 '05 #2
dharmesh Gupta wrote:
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_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.

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

Jul 19 '05 #3

"dharmesh Gupta" <dh************ @esteltelecom.c om> wrote in message
news:45******** *************** ***@posting.goo gle.com...
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_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,


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
Jul 19 '05 #4
Many thanks to you.

"John Harrison" <jo************ *@hotmail.com> wrote in message news:<bf******* *****@ID-196037.news.uni-berlin.de>...
"dharmesh Gupta" <dh************ @esteltelecom.c om> wrote in message
news:45******** *************** ***@posting.goo gle.com...
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_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,


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

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2094
by: Dan Williams | last post by:
Hi people I've just joined the Python list and although I'm sure my question must be asked fairly frequently, I have been unable to find an answer after two days of searching. For the record, my total Python experience is also two days :) (I come from a C++ background though, so I have good programming knowledge in general). My question is thus: how do I reference a parent module and its children?
3
1782
by: Olivier Noblanc ATOUSOFT | last post by:
Hello, When i want to import a .py fire from another subdirectory i make import inc/setupxml but that make me an error message.
1
1404
by: Ezekiël | last post by:
Hi, How can i import multiple accessfiles in sql server 2000 by using a dts package? The dts package should import files with the most current date in a directory (not today's date). The date can be found in the name, e.g.: <companyname>_20041214.mdb. Also i would like to use the companyname from the filename to fill in a
2
3164
by: Tmuld | last post by:
Hello! I have data exported from a Reference Manager 11, and need to import it into and SQL database. Each record has different number of fields. It is used to cite journal articles. (more about the format at http://www.adeptscience.co.uk/kb/article/A626)
1
3073
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
1
3187
by: enrio | last post by:
I have made a number of changes to the code of an access 2000 application, and now the person responsible for the application wants to import one change at a time into his "master copy", after he has understood the change. Most of the code in this application is in the forms. I find that I can export the code, and I can import the code of the modules, but I cannot import the code of the forms. I have exported the code of the forms to...
4
14907
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is strongly name which contains the class where the static variable is defined. This library can be referenced by multiple projects. I am fairly sure the static variable does not survive across the application boundry but does it within the application...
2
3490
by: aruna.mysore | last post by:
Hi all, I have specific question about usage of extern. I want to share a single variable say int a; across multiple files. I would define it in file1.c as int a=0; int main() {
6
26336
by: provor | last post by:
Hello, I have the following code that I am using when a user presses a button to import an excel file into a table. The code is hard coded to point to the correct table. This works great for this one table. My problem is I have two buttons I want to use this code for for the two buttons would put the data in different tables. I have tried copying and changing a few things and nothing will work for me. The code is set up in a module and then I...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9582
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10580
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7621
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3821
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.