#include <iostream>
#include <fstream>
int main (int argc, char *argv[])
{
std::cout << "Opening file named " << argv[1] << std::endl;
std::ifstream in(argv[1]);
std::string word;
in >> word;
std::cout << " First word is " << word << std::endl;
std::cout << "This program was called with " << argv[0];
return 0;
}
thx methodos &moschops for ur replies
but my problem isn't how to execute this program from the command window (tried to do it with another program and executed successfully)
this code when i try to debug it by visual studio 2008 it displays an error message says
Debug assertion failed
program:(the path of my execution file)
file:f:\dd\vctools\crt_bld\self_x86\crt\src\mbstowcs.c
line 69
expression s !=NULL
when trying to debug it step by step this message appears at the line of argv[1] but when i replace it by the name of my text file the code debug successful !!!
i thought that it may be from my stack but it work with the text file name normally !!!
Your program cannot be debugged. Period.
The reason is that the debugger needs a second parameter, argv[1], to be supplied to the program.
With your code, after the program is built, the debugger starts the program as "yourprogram.exe".
For your program to work, however, it needs two parameters. The first is argv[0] and that is "yourprogram.exe". The second parameter is argv[1], which is the file that needs to be opened "FileToBeOpened.extension".
Then,
1. make sure that both these files are in the same directory
2. open command promt
3. and type "yourprogram.exe FileToBeOpened.extension" in the right directory
Alternatively, replace argv[1] by the filename. The debugger will not need a second parameter. That's why it debugs fine as you already noted.
Have fun.
finally i knew my fault...... ooooooooooh it is a very stupid error
my execution file name have a space which the command window can't recognize the file name from the execution file name a very stupid error