0

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?

3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by mvmalderen
0

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

0

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";
}
1

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]);

Votes + Comments
Everyone makes mistakes :)
0

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

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.