Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > help needed with filnames as command line arguments

Reply
Thread Tools

help needed with filnames as command line arguments

 
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-17-2003
David Bernier <(E-Mail Removed)> wrote:
>
>Craigb wrote:
>
>>David Bernier <(E-Mail Removed)> wrote in message news:<_kMjb.10049$(E-Mail Removed) m>...
>>
>>>I'd like to pass on the command line two filenames.
>>>
>>>As for example:
>>>
>>>my_executable filename_1 filename_2

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

>/*** NOTE: I added #include <string.h> here ***/


You don't use anything defined in string.h, so why include it?

>>int printargstuff(int aa, char *bb[]);
>>
>>int main(int argc, char *argv[]) //NOTE - passing in command line argc/argv
>>{
>> printargstuff(argc, *argv[0]); //DB: line 9: Warning issued about this line, see below...


Make it match the prototype:

printargstuff(argc, argv);

<snip>
>It didn't quite work. I got a warning about this line:
>
>printargstuff(argc, *argv[0]); here it is:


See above.

<snip>

Regards
--
Irrwahn
((E-Mail Removed))
 
Reply With Quote
 
 
 
 
Dan Pop
Guest
Posts: n/a
 
      10-17-2003
In <_kMjb.10049$(E-Mail Removed)> David Bernier <(E-Mail Removed)> writes:

>I'd like to pass on the command line two filenames.
>
>As for example:
>
>my_executable filename_1 filename_2
>
>I haven't done any C programming with command line arguments so far.
>
>I'm familiar with fopen, ascii mode, binary mode. What I'm doing
>now is specifying the filenames in the source code.
>So I give different names to each source code file
>and recompile each time.
>
>The data in the files is signed ints and I read from files in ascii mode,
>not binary.
>
>Also, I'd be glad to have some references about
>command line argument programming or simple examples.
>
>I have K&R, Harbison and Steele's ``C A Reference Manual" and
>I know where to find the FAQ for comp.lang.c


What's the point in having C books if you can't be bothered to read them?
K&R explains the issue in great detail.

You also need to know that you have to read the FAQ *before* posting.
But your question is far too basic to be worth addressing in the FAQ:
any good tutorial C book will describe the parameters of the main
function and their usage.

#include <stdio.h>

int main(int argc, char **argv)
{
while (*argv) puts(*argv++);
return 0;
}

Couldn't be any easier.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Craigb
Guest
Posts: n/a
 
      10-18-2003
> <meta rant>
> I am impressed by the fact that only one out of four replies (so far)
> contained correct code. Please, test your code before you post it.
> Thank you.
> </meta rant>
>
> Regards


Tested... *still* works fine for me, however.. appreciate the
feedback. Tested on Bloodshed gcc and Cygwins gcc also in case you're
wondering.
/cb
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-18-2003
(E-Mail Removed) (Craigb) wrote:

>> <meta rant>
>> I am impressed by the fact that only one out of four replies (so far)
>> contained correct code. Please, test your code before you post it.
>> Thank you.
>> </meta rant>
>>
>> Regards

>
>Tested... *still* works fine for me, however.. appreciate the
>feedback. Tested on Bloodshed gcc and Cygwins gcc also in case you're
>wondering.


Really? Let's see...

Output generated by MingW32 (gcc 3.2, the version that comes with
Bloodshed DevCpp, so probably the same you use):

[...]
gcc.exe "D:\Temp\CraigB_1.c" -o "D:\Temp\CraigB_1.exe" -W -Wall
-std=c99 -O3 -I[...] -L[...]
[...]
D:/Temp/CraigB_1.c:8: warning: passing arg 2 of `printargstuff'
makes pointer from integer without a cast [1]

OK, now let's review your code:
....
003 int printargstuff(int aa, char *bb[]);
....
006 int main(int argc, char *argv[])
007 {
008 printargstuff(argc, *argv[0]);
....

The second argument of printargstuff() has to be a
pointer-to-pointer-to-character. You pass *argc[0] to it,
which is a character. Now please explain how this is
supposed to work without invoking undefined behaviour.


[1] You most probably didn't set your warning level to a
suitable amount. Warnings usually _are_ errors!

Regards
--
Irrwahn
((E-Mail Removed))
 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      10-18-2003
"David Bernier" <(E-Mail Removed)> wrote:
> I'd like to pass on the command line two filenames.
>
> As for example:
>
> my_executable filename_1 filename_2
>
> I haven't done any C programming with command line arguments so far.
>
> I'm familiar with fopen, ascii mode, binary mode. What I'm doing
> now is specifying the filenames in the source code.
> So I give different names to each source code file
> and recompile each time.
>
> The data in the files is signed ints and I read from files in ascii mode,
> not binary.


I think you mean text mode. There is no "ascii mode" in C.

C:\docs\prog\c>type dbernier.c
#include <stdio.h>

int main(int argc, char **argv)
{
int i;
for(i = 1; i < argc; i++)
{
int value = 0;
FILE *fp = fopen(argv[i], "r");
if(fp)
{
if(fscanf(fp, "%d", &value) == 1)
{
printf("%s: %d\n", argv[i], value);
}
fclose(fp);
}
}
return 0;
}

C:\docs\prog\c>gcc -std=c99 -pedantic -Wall -W -O2 dbernier.c -o dbernier

C:\docs\prog\c>dbernier *
42.txt: 69
comb.txt: 0
fig.out: 2147483647
input.raw: 6
lotto.diff: 1781
tmp2: 9

These files just happened to have a number at the beginning.

--
Simon.


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-18-2003
Simon Biber wrote (minisculy edited):
>

.... snip ...
>
> #include <stdio.h>
> int main(int argc, char **argv)
> {
> int i;
> for (i = 1; i < argc; i++) {
> int value = 0;
> FILE *fp = fopen(argv[i], "r");
> if (fp) {
> if (fscanf(fp, "%d", &value) == 1)) {
> printf("%s: %d\n", argv[i], value);
> }
> fclose(fp);
> }
> }
> return 0;
> }


While C has, for some time, allowed this sort of declaration
within a block, I see very little use for it. The code is much
more flexible if broken into a separate routine. The only cost is
(possibly) the time to call and return, which would be vanishingly
small here compared with the actual routine action. Thus I would
recommend:

#include <stdio.h>
static void tryfile(const char *fn)
{
int value;
FILE *fp;
if (fp = fopen(fn, "r"))
if (fscanf(fp, "%d", &value) == 1) {
printf("%s: %d\n", fn, value);
}
fclose(fp);
}
}

int main(int argc, char **argv)
{
int i;
for (i = 1; i < argc; i++) {
tryfile(argv[i]);
}
return 0;
}

which, at least to me, is clearer in that each routine has one
simple responsibility. With C99 you can also have all the
benefits of the single routine method by using the inline
directive. YMMV.

With this organization it is easier (to me) to see that one should
worry about the action of fopen on an empty string, and trivial to
avoid it. We can also easily see that initialization during
declaration is not needed anywhere.

There is no need to agree with me but at least you can
evaluate my preference after seeing the reasons.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-18-2003
CBFalconer <(E-Mail Removed)> wrote:

>Simon Biber wrote (minisculy edited):
>>

>... snip ...
>>
>> #include <stdio.h>
>> int main(int argc, char **argv)
>> {
>> int i;
>> for (i = 1; i < argc; i++) {
>> int value = 0;
>> FILE *fp = fopen(argv[i], "r");
>> if (fp) {
>> if (fscanf(fp, "%d", &value) == 1)) {
>> printf("%s: %d\n", argv[i], value);
>> }
>> fclose(fp);
>> }
>> }
>> return 0;
>> }

>
>While C has, for some time, allowed this sort of declaration
>within a block, I see very little use for it.


Alas, I sometimes use initialization within a block in switch
statements, e.g. where a helper variable is needed in only one
special case and I want to have it declared where it's used:

/*...*/
int foo, bar;

switch( whatever )
{
case CASE_X:
/* do something */
break;

case CASE_SWAP:
{
int tmp = foo;
foo = bar;
bar = tmp;
}
break;

case CASE_Y:
/* do something else */
break;

default:
/* perform default action */
}


Regards
--
Irrwahn
((E-Mail Removed))
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-19-2003
Irrwahn Grausewitz wrote:
> CBFalconer <(E-Mail Removed)> wrote:
>

.... snip ...
> >
> > While C has, for some time, allowed this sort of declaration
> > within a block, I see very little use for it.

>
> Alas, I sometimes use initialization within a block in switch
> statements, e.g. where a helper variable is needed in only one
> special case and I want to have it declared where it's used:


You snipped my point, which was that such usage often indicates
the need to repartition the code.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-19-2003
CBFalconer <(E-Mail Removed)> wrote:

>Irrwahn Grausewitz wrote:
>> CBFalconer <(E-Mail Removed)> wrote:
>>

>... snip ...
>> >
>> > While C has, for some time, allowed this sort of declaration
>> > within a block, I see very little use for it.

>>
>> Alas, I sometimes use initialization within a block in switch
>> statements, e.g. where a helper variable is needed in only one
>> special case and I want to have it declared where it's used:

>
>You snipped my point, which was that such usage often indicates
>the need to repartition the code.


I snipped it because your point remains valid and I support it, but in
special cases I ignore it, when I don't want to repartition the code.

Admittedly my actual example was a rather silly one. A more realistic
example would've been a message handler routine, but I didn't want to
clutter my post with ugly OT windowish code.

Regards
--
Irrwahn
((E-Mail Removed))
 
Reply With Quote
 
Craigb
Guest
Posts: n/a
 
      10-20-2003
> The second argument of printargstuff() has to be a
> pointer-to-pointer-to-character. You pass *argc[0] to it,
> which is a character. Now please explain how this is
> supposed to work without invoking undefined behaviour.
>
>
> [1] You most probably didn't set your warning level to a
> suitable amount. Warnings usually _are_ errors!
>
> Regards


There you go, now I learned something!..
I re-did it in the ide and of course found what you are saying. I
won't go too far off topic, but the reason I was in the threads at all
was to find out exactly that sort of thing, so no-bad from my point of
view. Of course now I'll be doing searches on your login to find out
what other tips I can glean.
/cb.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Filnames and timestamps milkyway C Programming 8 09-19-2005 08:14 AM
Run Unix shell command $ parse command line arguments in python rkoida@yahoo.com Python 4 04-23-2005 04:42 AM
Documentation on command line arguments to perl Peter Kay Perl 1 05-18-2004 01:27 AM
How to hide command line arguments in a windows application SC C++ 2 05-05-2004 10:06 AM
Parser for command line arguments? Ahmed Moustafa Java 0 08-21-2003 05:21 AM



Advertisments