473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Main [was File IO]

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

int main(int argc, char *argv[]) {
FILE *in, *out;

if(argc==1)
{
in = stdin;
out = stdout;
}
else
{
if(argc==2)
{
in = stdin;
if( (out = fopen(argv[1], "w"))==NULL )
{
perror("Output file creation");
exit(EXIT_FAILURE);
}
}
else
{
if( (in = fopen(argv[1], "r"))==NULL )
{
perror("Input file opening");
exit(EXIT_FAILURE);
}
if( (out = fopen(argv[2], "w"))==NULL )
{
perror("Output file creation");
fclose(in);
exit(EXIT_FAILURE);
}
}
}

/* some code here */

return EXIT_SUCCESS;
}

I'm looking at Giannis's code here and I see the one thing in C I dread the
most. I avoid even looking at it if I can. But one must learn main (int
argc, char *argv[]). Those main parameters look like garbage to me, when
would one use them. Argc must mean an argument to main, the array pointer to
a char pointer leaves me clueless. I understand main(void). What's the
purpose of the other parameters?

Bill

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 14 '05 #1
12 1719
>Those main parameters look like garbage to me, when
would one use them. Argc must mean an argument to main, the array pointer to
a char pointer leaves me clueless. I understand main(void). What's the
purpose of the other parameters?


Suppose what happens when you type

ls -l

Jan Engelhardt
--
Nov 14 '05 #2
Bill Cunningham wrote:

I'm looking at Giannis's code here and I see the one thing in C I dread the
most. I avoid even looking at it if I can. But one must learn main (int
argc, char *argv[]). Those main parameters look like garbage to me, when
would one use them. Argc must mean an argument to main, the array pointer to
a char pointer leaves me clueless. I understand main(void). What's the
purpose of the other parameters?

Bill


Bill, the argc parameter tells the number of command line arguments that
where passed to the program (including program name)... The char *argv[]
is an array to char* that holds the arguments...

So strcmp(argv[0], "myprog") is always 0 in our case and the last
element is argv[argc-1]...

That is, being your "myprog", if you execute

# myprog

then

argc = 1; and argv[] = { "myprog" };

If you exec

# myprog i am a c freak

then

argc = 6; and argv[] = { "myprog", "i", "am", "a", "c", "freak" };

See K&R (they explain it more than enough).

--
#include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v?p(Little):p(Big);return 0;}

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 14 '05 #3

"Bill Cunningham" <no****@nspam.net> wrote in message
news:40********@corp.newsgroups.com...
Re: Main [was File IO]
Since your question is about an entirely different topic
from the original one of this thread, imo you should have
started a new thread. Whatever... See below.
int main(int argc, char *argv[]) {

I'm looking at Giannis's code here and I see the one thing in C I dread

the most. I avoid even looking at it if I can. But one must learn main (int
argc, char *argv[]). Those main parameters look like garbage to me, when
would one use them. Argc must mean an argument to main, the array pointer to a char pointer leaves me clueless. I understand main(void). What's the
purpose of the other parameters?


They're for accessing a program's (typically 'command line')
parameters.

================================================== ============

ISO/IEC 9899:1999 (E)

[....]

5.1.2.2.1 Program startup

1 The function called at program startup is named main. The
implementation declares no prototype for this function. It
shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv,
though any names may be used, as they are local to the
function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;(9) or in some other implementation-defined
manner.

2 If they are declared, the parameters to the main function
shall obey the following constraints:

-- The value of argc shall be nonnegative.

-- argv[argc] shall be a null pointer.
-- If the value of argc is greater than zero, the array
members argv[0] through argv[argc-1] inclusive shall
contain pointers to strings, which are given implemen-
tation-defined values by the host environment prior to
program startup. The intent is to supply to the program
information determined prior to program startup from
elsewhere in the hosted environment. If the host envi-
ronment is not capable of supplying strings with letters
in both uppercase and lowercase, the implementation shall
ensure that the strings are received in lowercase.

-- If the value of argc is greater than zero, the string
pointed to by argv[0] represents the program name;
argv[0][0] shall be the null character if the program name
is not available from the host environment. If the value
of argc is greater than one, the strings pointed to by
argv[1] through argv[argc-1] represent the program
parameters.

-- The parameters argc and argv and the strings pointed to
by the argv array shall be modifiable by the program,
and retain their last-stored values between program startup
and program termination.

[....]

(9) Thus, int can be replaced by a typedef name defined as int,
or the type of argv can be written as char ** argv, and so on.

[....]

================================================== ============
Example use:

#include <stdio.h>

int main(int argc, char **argv)
{
const char * const messages[] =
{
"[not available]",
"[none given]",
""
};

const char * const *pmsg[] = {messages, argv};
int i = 0;
printf("argc == %d\n\n", argc);

printf("Program name:\n%s\n\n",
argc > 0 ? *pmsg[**argv != 0]
: messages[argc > 0]);

printf("Parameters:\n");

for(i = 1; i < argc; ++i)
printf("[%d] == %s\n", i, argv[i]);

printf("%s\n", messages[(argc > 1) + (argc > 0)]);
return 0;
}

-Mike
Nov 14 '05 #4
"Papadopoulos Giannis" <ip******@inf.uth.gr> wrote in message
news:c0***********@ulysses.noc.ntua.gr...
Bill Cunningham wrote:

...But one must learn main (int argc, char *argv[]). Those main
parameters look like garbage to me, when would one use them. ...
Bill, the argc parameter tells the number of command line arguments that
where passed to the program (including program name)... The char *argv[]
is an array to char* that holds the arguments...

So strcmp(argv[0], "myprog") is always 0 in our case and the last
element is argv[argc-1]...


No and no.

The argc parameter must be non-negative, but could be 0. The only guarantee
you have is that argv[argc] == NULL.

Even if argc is non-zero, argv[0] may be practically anything, including an
empty string.
That is, being your "myprog", if you execute

# myprog

then

argc = 1; and argv[] = { "myprog" };


Not necessarily. The following are all possible too...

argc == 0
argc == 1 && argv[0][0] == 0
argc == 1 && strcmp("/usr/local/bin/myprog", argv[0]) == 0
argc == 1 && strcmp("myprog.exe", argv[0]) == 0
argc == 1 && strcmp("C:\\MYPROGS\\MYPROG.EXE", argv[0]) == 0

--
Peter
Nov 14 '05 #5

"Papadopoulos Giannis" <ip******@inf.uth.gr> wrote in message
news:c0***********@ulysses.noc.ntua.gr...
Bill Cunningham wrote:

I'm looking at Giannis's code here and I see the one thing in C I dread the most. I avoid even looking at it if I can. But one must learn main (int
argc, char *argv[]). Those main parameters look like garbage to me, when would one use them. Argc must mean an argument to main, the array pointer to a char pointer leaves me clueless. I understand main(void). What's the
purpose of the other parameters?

Bill
Bill, the argc parameter tells the number of command line arguments that
where passed to the program (including program name)... The char *argv[]
is an array to char* that holds the arguments...

So strcmp(argv[0], "myprog") is always 0 in our case and the last
element is argv[argc-1]...

That is, being your "myprog", if you execute

# myprog

then

argc = 1; and argv[] = { "myprog" };


Not necessarily. argc is allowed to be zero (but not negative),
in which case no program name or parameters are available.
Also, even if argc > 0, argc[0][0] is allowed to be zero if
program name is not available. See the ISO quote in my
other post this thread.

If you exec

# myprog i am a c freak

then

argc = 6; and argv[] = { "myprog", "i", "am", "a", "c", "freak" };


Not necessarily. The first element could be "\0";

-Mike
Nov 14 '05 #6
Peter Nilsson wrote:
Even if argc is non-zero, argv[0] may be practically anything, including an
empty string.


Never occured to me... The more I grow the more I learn...

--
#include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v?p(Little):p(Big);return 0;}

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 14 '05 #7
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:ZH*******************@newsread1.news.pas.eart hlink.net...
....
argc = 6; and argv[] = { "myprog", "i", "am", "a", "c", "freak" };


Not necessarily. The first element could be "\0";


True, but I think it's more likely to be "" than "\0", although I can't
really justify the statement in any objective fashion. <g>

--
Peter
Nov 14 '05 #8
Mike Wahler wrote:

"Papadopoulos Giannis" <ip******@inf.uth.gr> wrote in message
news:c0***********@ulysses.noc.ntua.gr...
If you exec

# myprog i am a c freak

then

argc = 6; and argv[] = { "myprog", "i", "am", "a", "c", "freak" };


Not necessarily. The first element could be "\0";


And the /last/ element /must/ be NULL.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #9
"Peter Nilsson" <ai***@acay.com.au> wrote in message
news:40******@news.rivernet.com.au...
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:ZH*******************@newsread1.news.pas.eart hlink.net...
...
argc = 6; and argv[] = { "myprog", "i", "am", "a", "c", "freak" };


Not necessarily. The first element could be "\0";


True, but I think it's more likely to be "" than "\0", although I can't
really justify the statement in any objective fashion. <g>


Sloppy presentation on my part. I did mean an 'empty string',
but was trying to be clearer by inserting the '\0' character.
Not the first time one of my attempts at clarity resulted
in the opposite. :-)

-Mike
Nov 14 '05 #10
ls -l

Jan Engelhardt


In a UNIX or LINUX environment a directory will be listed. At least on my
linux OSs it's ls -la.

Bill

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 14 '05 #11
"Bill Cunningham" <no****@nspam.net> wrote in message
news:40********@corp.newsgroups.com...
ls -l

Jan Engelhardt


In a UNIX or LINUX environment a directory will be listed. At least on my
linux OSs it's ls -la.


I think you've missed the point. In an 'ls' written in C,
That invocation would cause argc to have a value of 2,
argv[0] would be either "" or "ls", and argv[1] would
be "-l". IOW argc/argv is how the program retrieves
user supplied parameters.

-Mike
Nov 14 '05 #12
> I think you've missed the point. In an 'ls' written in C,
That invocation would cause argc to have a value of 2,
argv[0] would be either "" or "ls", and argv[1] would
be "-l". IOW argc/argv is how the program retrieves
user supplied parameters.

-Mike
I see.



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 14 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: G Patel | last post by:
Can anways explain why many multi-file programs I've seen don't have a main.c file, instead main() is hidden in a file with a different name? Is this just a preference, or is there an unspoken rule...
3
by: Pavel | last post by:
Hi I'm new with vb.net I've got application of single form 2 buttons and text field When I try to run my progra I get the following messag 'Sub Main' was not found in 'LoanCalculator.Form1' ...
17
by: GS | last post by:
the main menu in the application seemed to disappeared all together until I click on an control and select mainmenu1 in designer. then the mainmenu1 displays where it should be but running it or...
4
by: Learner | last post by:
Hello, I just wanted to jump on try creating a small calss in VB.NET in a note pad. And my class takes below lines of code Public Class Product Public Name as string
2
by: Joseph Geretz | last post by:
When I create a Form, the VB IDE creates the following files in the following hierarchy: Form1.cs Form1.Designer.cs Form1.resx Both Form1.cs and Form1.Designer.cs are partial implementations...
5
by: ron.longo | last post by:
Is there any way that I can find the path of the main .py file of my application? For example, I have an application with some resources which are in a subdirectory: myPythonApp.py...
1
by: stepthom | last post by:
I am attempting to break my templates down from 1 main xsl file into smaller files. Then I want to call them from a main xsl file. I started to look. I have found xsl:include and xsl:import. Also,...
0
by: premMS143 | last post by:
Hi Everyone, Wish u a Happy new year. I'm new to Flash. However know it lightly. I've 10 SWF files which was converted from powerpoint using 3rd party software. Now I want to create a Main...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.