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