Hello there,
I am pretty new at this C/C++ I am writing a console program, and from what I read here, and around the internet is that the argv array in main() is supposed to hold any arguments entered on the command line... argv[0] is the name of the program, and argv[1] to argc is the arguments. So if I were to write a program that opened a file in binary mode. To run it I would type executable and then the file I want to open and operate on.
example: C:// binaryopen binaryfile.bin
binaryopen is the name of the program, argv[0]
binaryfile.bin is the file being operated on, argv[1]
at least that's what the internet sites have been telling me... It seems to work except, that argv[ ] in any position 0-1-2, ect only returns the very first letter in the char array. So my program reports it can't find the file as it's coded to and continues execution from there. I have it opening a test file and performing the operation. This works as it's coded
fopen ( "sample.bmp", "rb");
opens it no problem. but if I try it from the command line in the exact same directory as both the executable and the test file by typing it it will tell me File not Found anyway.
C:// binaryopen sample.bmp
output
argv[1] = s File Not Found.
Sorry if this is lengthy. From what I have read so far this should work. But isn't...
Is there something I'm missing
//open the filestream
if ( argv[1] != NULL )
{
printf( "argv is not null argv = %s \n", argv[0] );
bmpstream = fopen(argv[1], "rb");
}
else if ( argv[1] == NULL )
{
printf( "argv = null opening the sample bmp \n" );
bmpstream = fopen( "sample.bmp", "rb" );
}
//If load is unsuccessful say so and quit
if (bmpstream == NULL)
{
printf( "%s File not Found. Please check the filename and try again. \n", argv[1] );
getchar();
return 0;
}

Problem using argv in main
Parrot88
It is probably better to use argc to do the same thing. So your code would be something like:
#include <stdio.h>
int main( int argc, char *argv[] )
{
FILE* bmpstream;
if ( argc > 1 )
{
printf( "argv is not null argv = %s and number of arguments %d \n", argv[1],argc );
bmpstream = fopen(argv[1], "rb");
}
else
{
printf( "no argv so use default file \n" );
bmpstream = fopen( "sample.bmp", "rb" );
}
//If load is unsuccessful say so and quit
if (bmpstream == NULL)
{
printf( "%s File not Found. Please check the filename and try again. \n", argv[1] );
}
}
Hope this helps!
Thanks,
Ayman Shoukry
VC++ Team
kvkris
Chunmun
Well it seems that didn't help. Here something I noticed and need more clarification on. When I print argv say argv = Variable.
printf( "argv is a big %s!!", argv[1]); //as an example
I get this output
Argv is a big V!!
If I use the %S <<capital S.
printf( "argv is a big %S!!", argv[1]); // capital S instead...
I get output
Argv is a big Variable.
What is causing this and is this happening when openf trys using argv also.
It still tells me File not Found....
Taslim
Did you try the bove sample I posted. It wors as expected for me. IS it working as expected for you
Could you post the exact sample you are suing including all details with how your main method is defined
Thanks, Ayman Shoukry VC++ Teamneil_fl
No it didn't I think I alternated my code to your suggestion. Here is the portion of my main that is giving me all the trouble. It doesn't recognize what ever argv is holding as a valid filename name. Even if it's run from the same directory as the file.
#include
"stdafx.h"#include
"stdio.h"#include
"Windows.h"#include
"WinDef.h"int
_tmain(int argc, char* argv[]){
FILE *bmpstream;
//structures included in WINDOWS.H files no need to redo.BITMAPFILEHEADER Bmfh;
BITMAPINFOHEADER Bmih;
//open the filestream if ( argc > 1 ){
printf(
"argv is not null argv = %S. The number of arguments = %d \n", argv[1], argc );bmpstream = fopen(argv[1],
"rb");}
else // ( argv[1] == NULL ){
printf(
"argv = null opening the sample bmp \n" );bmpstream = fopen(
"sample.bmp", "rb" );}
//If load is unsuccessful say so and quit if (bmpstream == NULL){
printf(
"%S File not Found. Please check the filename and try again. \n", argv[1] );getchar(); //here to keep the console window open in debug
return 0;}
Clement M
I suspect that you have created a Unicode project (I see that main is _tmain) hence the arguments strings that are passed into the program are in Unicode. For output you are using printf and %s which is for ASCII strings and won't work for Unicode strings. You seem to have found one solution for this (use %S which is for printing out Unicode strings) but for a more general solution you should take a look at _tprintf
http://msdn2.microsoft.com/en-us/library/wc7014hz(VS.80).aspx
MarinaNZ
Did you try _tfopen
http://msdn2.microsoft.com/en-us/library/yeby3zcb(VS.80).aspx