| | |
writing a simple cat program
![]() |
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
If someone would please help me get started I would appreciate it!
thanks
>I am confused about where it will be getting files from?
The program will be executed like this:
For the purposes of your assignment, you can assume the following for argc and argv of main:
To test this out, use the following program:
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.
The program will be executed like this:
C Syntax (Toggle Plain Text)
$ mycat file1 file2 file3
C Syntax (Toggle Plain Text)
argc = 4; argv[0] = "mycat"; argv[1] = "file1"; argv[2] = "file2"; argv[3] = "file3";
C Syntax (Toggle Plain Text)
#include <stdio.h> int main ( int argc, char *argv[] ) { int i; printf ( "argc = %d\n", argc ); for ( i = 0; i < argc; i++ ) printf ( "argv[%d] = %s\n", i, argv[i] ); return 0; }
I'm here to prove you wrong.
•
•
•
•
Originally Posted by Narue
>I am confused about where it will be getting files from?
The program will be executed like this:
For the purposes of your assignment, you can assume the following for argc and argv of main:C Syntax (Toggle Plain Text)
$ mycat file1 file2 file3
To test this out, use the following program:C Syntax (Toggle Plain Text)
argc = 4; argv[0] = "mycat"; argv[1] = "file1"; argv[2] = "file2"; argv[3] = "file3";
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.C Syntax (Toggle Plain Text)
#include <stdio.h> int main ( int argc, char *argv[] ) { int i; printf ( "argc = %d\n", argc ); for ( i = 0; i < argc; i++ ) printf ( "argv[%d] = %s\n", i, argv[i] ); return 0; }
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!
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".
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".
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:
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:
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: fin troubles
- Next Thread: Do variable names affect a program?
| Thread Tools | Search this Thread |
* adobe ansi api array binarysearch centimeter changingto char character cm convert copyanyfile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory feet fflush fgets file floatingpointvalidation fork frequency function givemetehcodez global grade graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o inches infiniteloop input interest intmain() iso keyboard kilometer linked linkedlist linux linuxsegmentationfault list looping loopinsideloop. lowest match meter microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf posix power process program programming pyramidusingturboccodes read recv recvblocked repetition research reversing scanf scheduling segmentationfault send single socket socketprograming socketprogramming stack standard string suggestions threads unix urboc user voidmain() whythiscodecausesegmentationfault win32api windows.h windowsapi






