i have to read input from .sql file .but that file name should be read from command line instead of cin.how to do that command?
if my file t.sql ,how it would be?
i tried like this but i couldnt get the output
int main(int argc, char* argv[])
which file i need to execute?
g++ a.cpp
or g++ t.sql?

Recommended Answers

All 4 Replies

Try something like this:

char filename[20];
if(argc>1)
{
    strcpy(filename, argv[1]);
}

Edit:: We have to check whether the number of arguments is greater than 1 because the program's first argument ( argv[0] ) is always containing the program's filename ...

when you type the filename on the command line that name (assuming it has no spaces) is contained in argv[1]

int main( int argc, char* argv[])
{
   if( argc > 1)
      cout << "The file name is: " << argv[1] << "\n";
}

Try something like this:

char filename[20];
if(argc>1)
{
    filename = argv[1];
}

In your example you have to use strcpy() strcpy(filename, argv[1]);

commented: Everyone makes mistakes :) +4

In your example you have to use strcpy() strcpy(filename, argv[1]);

Sorry, I forgot :'(
Edit:: Corrected it in my post :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.