Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

main(int argc, char *argv[])

Status
Not open for further replies.

darceo

Programmer
Sep 30, 2002
3
0
0
US
Ok, i've never ran a file that contains and argument in the main such as main(*****,****). When you compile the functin in Linux one types 'gcc foo.c' but then when i try and run the file './a.out' I notice that you need more than this to run the program. Ok I'm not making any sense but what I need help in is for example the program below lets call it foo.c how would i run it without getting the message "You forgot to enter the filename"

thanks

#include <stdio.h>
#include <stdlib.h>

void main(int argc, char *argv[])
{
FILE *in, *out; // file pointers
char ch;

if(argc!=3)
{
printf(&quot;You forgot to enter the filenames\n&quot;);
exit(1);
}

if((in=fopen(argv[1], &quot;rb&quot;))==NULL) // open source file
{ // for read binary
printf(&quot;Cannot open source file\n&quot;);
exit(1);
}

if((out=fopen(argv[2], &quot;wb&quot;))==NULL) // open dest file
{ // for write binary
printf(&quot;Cannot open destination file\n&quot;);
exit(1);
}

while(!feof(in)) // here it is
{
ch=getc(in);
if(!feof(in)) // and again
putc(ch,out);
}

fclose(in); // close files
fclose(out);
}
 
Stupid question :
Do you actually supply arguments to the program when you run it ?

foo file1 file2

Try to printf argc before you use it.
/JOlesen
 
Half humorous, but I hope you get the idea...

if (!argc) {
printf(&quot;Oh well, using defaults.\n&quot;);
argv[1] = &quot;defaultfile1&quot;
argv[2] = &quot;defaultfile2&quot;
}
 
marsd

1) argc will be 1 if no command line args are supplied so your first test will never succeed

2) your assignments will attempt to access non-existant elements of an array - seg fault time.....

Cheers
 
yeah, i thought of that, bad example, you would
have to be my bad conscience on this ;)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
int noargs = 0;
if (argc < 3) {
argv[1] = malloc(50);
argv[2] = malloc(50);
/*Option to the static method.
*printf(&quot;File to load(?): &quot;);
*scanf(&quot; %50s&quot;, argv[1]);
*printf(&quot;File to load(?): &quot;);
*scanf(&quot; %50s&quot;, argv[2]);
*/
strcpy(argv[1],&quot;defaultfile&quot;);
strcpy(argv[2],&quot;defaultfile2&quot;);
noargs = 1;
}

//rest of code here...
//cleanup
if (noargs > 0) {
free(argv[1]);
free(argv[2]);
}
return 0;
}
 
i actually found that example on the web and compiled it and couldn't get it to work thus the reason for my post. when i did that i was trying to learn how the write to a file, open a file and so forth. I was just curios, i am not a profesional programer or anything like that. now i know all you do is foo file1 file2
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top