LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-31-2009, 08:16 AM   #1
erwfili
LQ Newbie
 
Registered: Oct 2009
Posts: 13

Rep: Reputation: 0
arguments.......questions!!


i am trying to understand a c code for the arguments in C but i get a bit confused....i will be pleased if you answer me some questions for the code....
1. why in function myRead the programmer uses two files as a parameter of the function???i only have one file to open....
2.while(argc==1) the parameters of the function is myread(stdin,stdout) but in else is myread(fp,stdout)...why this is different????
3. if i want to read from a file...can i always use the function myread as a standard function for read from a file???

Code:
#include <stdio.h>

void myRead(FILE *, FILE *);

int main(int argc, char *argv[]) {
	FILE *fp;
    	if(argc == 1)
		myRead(stdin, stdout);
    	else
		while(--argc > 0)
			if((fp = fopen(*++argv, "r")) == NULL) {
				printf("Can't open file: %s\n", *argv);
				return 1;
			}
			else {
				myRead(fp, stdout);
			
				fclose(fp);
			}
    	return 0;
}
    
void myRead(FILE *fpe, FILE *fps) {
	int c;
    	while((c = getc(fpe)) != EOF)
		putc(c, fps);
}

i will be thankfull if you help me cause i am not standing computer science...but i have a midterm on wedsday in c..
 
Old 10-31-2009, 08:25 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by erwfili View Post
1. why in function myRead the programmer uses two files as a parameter of the function???i only have one file to open....
Do you understand what the function does? If you do, it should be clear why there are two arguments.

Quote:
2.while(argc==1) the parameters of the function is myread(stdin,stdout) but in else is myread(fp,stdout)...why this is different????
Do you know about the arguments to main (i.e. argc and argv)? If not, learn about them and you should be able to work out what's going on.

Last edited by Nylex; 10-31-2009 at 08:26 AM.
 
Old 10-31-2009, 08:31 AM   #3
erwfili
LQ Newbie
 
Registered: Oct 2009
Posts: 13

Original Poster
Rep: Reputation: 0
i know what does the arguments means but i get a bit confused...thanks for your help....
 
Old 10-31-2009, 08:33 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
For starters, where did you get this code?

The function is defined such that it gets characters from one file and the sends them (put) to another file. That is why there are two arguments.

When the function is called, you have to tell it the source and destination. In one case, it is simply "stdin" and "stdout" (which I assume are defined in stdio.h). In the other case, "fp" is used instead of "stdin". Which option gets used is determined by the number of arguments sent to main().


For context, please tell us where you are in your studies--eg is this your first class in C?

Last edited by pixellany; 10-31-2009 at 08:37 AM. Reason: typo
 
Old 10-31-2009, 08:38 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by erwfili View Post
i know what does the arguments means but i get a bit confused...thanks for your help....
You need to say what you get confused about, because how else will we know?

Quote:
Originally Posted by pixellany View Post
For starters, where did you get this code?

The function is defined such that it gets characters from one file and the sends them "put" to another file. That is why there are two arguments.

When the function is called, you have to tell it the source and destination. In one case, it is simply "stdin" and "stdout" (which I assume are defined in stdio.h). In the other case, "fp" is used instead of "stdin". Which option gets used is determined by the number of arguments sent to main().
I suppose you and I have different approaches to "teaching". I prefer to gauge what they know and get them to think a bit .
 
Old 10-31-2009, 08:43 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by Nylex View Post
I suppose you and I have different approaches to "teaching". I prefer to gauge what they know and get them to think a bit .
I'm making no attempt at teaching. If I were, there would be a very different dialog.

I make quick judgement calls about the appropriate response to a question. I this case, it was obviously homework, but there was also evidence of prior effort. I posted what seemed to me to be the best way to get OP going in the right direction......YMMV
 
Old 10-31-2009, 08:47 AM   #7
erwfili
LQ Newbie
 
Registered: Oct 2009
Posts: 13

Original Poster
Rep: Reputation: 0
Code:
if(argc == 1)
		myRead(stdin, stdout);
this means that if there is not any file in command line the only elements excists is the name of the programme e.g. ./programme ,why we read from a file....while the file does not excists....??
 
Old 10-31-2009, 08:53 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
You lost me......That code fragment says that if the argument count is 1, then execute "MyRead" using stdin and stdout. In that case it is not necessarily reading from a file, but from whatever stdin has be defined to be----ie typically the keyboard.

As I requested previously, please tell us more about the context of your questions.
 
Old 10-31-2009, 08:57 AM   #9
erwfili
LQ Newbie
 
Registered: Oct 2009
Posts: 13

Original Poster
Rep: Reputation: 0
ok i think i am in a good way....thanks a lot for your help and for the time that you dedicated to me!!!i hope to make it up!!
 
Old 11-01-2009, 08:02 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
dealing with arguments hammertime1983 Programming 2 11-02-2007 08:43 AM
Shell: Getting Arguments kalyanofb Programming 4 03-01-2007 11:20 AM
What do these kernel arguments mean? TruongAn Linux - Software 2 10-20-2005 09:22 AM
Kernel arguments? ace_bandit_1 Linux - Hardware 2 06-29-2004 02:06 AM
More arguments in printf() AMMullan Programming 3 02-23-2004 02:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration