writing a simple cat program

Reply

Join Date: Nov 2004
Posts: 6
Reputation: kiki021600 is an unknown quantity at this point 
Solved Threads: 0
kiki021600's Avatar
kiki021600 kiki021600 is offline Offline
Newbie Poster

writing a simple cat program

 
0
  #1
Nov 11th, 2004
hi I am trying to write a simple cat program in C but am getting very confused. I thought i knew where to start but I dont. the assignment says: You will be writing a simple version of the program "cat". It must accept as arguments any number fo filenames. Each file's contents should be printed to stdout. Errors go to stderr, including if you cannot open a file. Do not crash if a file is not found, do continue to other files if one is not found. I am confused about where it will be getting files from? and how do i get things to print to stderr?:rolleyes:
If someone would please help me get started I would appreciate it!
thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: writing a simple cat program

 
0
  #2
Nov 11th, 2004
>I am confused about where it will be getting files from?
The program will be executed like this:
  1. $ mycat file1 file2 file3
For the purposes of your assignment, you can assume the following for argc and argv of main:
  1. argc = 4;
  2. argv[0] = "mycat";
  3. argv[1] = "file1";
  4. argv[2] = "file2";
  5. argv[3] = "file3";
To test this out, use the following program:
  1. #include <stdio.h>
  2.  
  3. int main ( int argc, char *argv[] )
  4. {
  5. int i;
  6.  
  7. printf ( "argc = %d\n", argc );
  8. for ( i = 0; i < argc; i++ )
  9. printf ( "argv[%d] = %s\n", i, argv[i] );
  10.  
  11. return 0;
  12. }
That's how you access command line arguments. Converting the above loop to open the files in sequence and either print the contents of the file to stdout or move on to the next file if it fails to open is fairly trivial if you know how to open a file and read from it.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6
Reputation: kiki021600 is an unknown quantity at this point 
Solved Threads: 0
kiki021600's Avatar
kiki021600 kiki021600 is offline Offline
Newbie Poster

Re: writing a simple cat program

 
0
  #3
Nov 11th, 2004
Originally Posted by Narue
>I am confused about where it will be getting files from?
The program will be executed like this:
  1. $ mycat file1 file2 file3
For the purposes of your assignment, you can assume the following for argc and argv of main:
  1. argc = 4;
  2. argv[0] = "mycat";
  3. argv[1] = "file1";
  4. argv[2] = "file2";
  5. argv[3] = "file3";
To test this out, use the following program:
  1. #include <stdio.h>
  2.  
  3. int main ( int argc, char *argv[] )
  4. {
  5. int i;
  6.  
  7. printf ( "argc = %d\n", argc );
  8. for ( i = 0; i < argc; i++ )
  9. printf ( "argv[%d] = %s\n", i, argv[i] );
  10.  
  11. return 0;
  12. }
That's how you access command line arguments. Converting the above loop to open the files in sequence and either print the contents of the file to stdout or move on to the next file if it fails to open is fairly trivial if you know how to open a file and read from it.

Thanks so much I will try that. One more question. when i delcare file 1, 2 and 3 as argc 1 2 and 3, does it have to contain something? like should i name file 1 2 and 3 something already in my list of programs? what i mean is my professor put this in the assignment window and it supposed to help but it is what is confusing me bc where do the files come from..

"If file1 exists and has "hello" and "world" on two lines, and file2 does not exist, here are example executions:

mycat file1 file2
hello
world
file2 does not exist

mycat file1 file2 2>file3
hello
world

mycat file3
file2 does not exist

mycat file1 file2 >file4
file2 does not exist

mycat file4
hello
world "

how come file 4 exists and doesnt say that file 2 doesnt exist? but file 3 says file 2 doesnt exist? still kinda confused, but thanks for the help you already gave me!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: writing a simple cat program

 
0
  #4
Nov 11th, 2004
If you have a question with the assignment definition itself, the best person to ask is your professor.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,144
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: writing a simple cat program

 
0
  #5
Nov 12th, 2004
Quite simple. The execution "mycat file2 2>file3" places the error output from "mycat file2" into the file "file3".
As file2 doesn't exist this new file will contain the text "file2 doesn't exist".

Remember the assignment says to print error output to stderr? What you do with 2>file3 is redirect stderr from the console to a file named "file3".
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6
Reputation: kiki021600 is an unknown quantity at this point 
Solved Threads: 0
kiki021600's Avatar
kiki021600 kiki021600 is offline Offline
Newbie Poster

Re: writing a simple cat program

 
0
  #6
Nov 15th, 2004
Hi
I tried what you said, and when i compiled it told me that i was passing arg from an incompatible pointer. I am so confused, I dont even know what that means. This is my first time ever doing programming and I am totally lost. I alos didnt understand what u meant when u said:That's how you access command line arguments. Converting the above loop to open the files in sequence and either print the contents of the file to stdout or move on to the next file if it fails to open is fairly trivial if you know how to open a file and read from it. What does that mean? I know that is probably a dumb question but i am so lost and I am supposed to hand this in today. I tried getting help from my prof. but he is an adjunct and never around!
Please help :rolleyes:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: writing a simple cat program

 
0
  #7
Nov 15th, 2004
>it told me that i was passing arg from an incompatible pointer
This doesn't tell me anything. What code did you use? What was the exact error?

>What does that mean?
It means I did everything for you except open the files and print them to the screen.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC