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

>I am confused about where it will be getting files from?
The program will be executed like this:

$ mycat file1 file2 file3

For the purposes of your assignment, you can assume the following for argc and argv of main:

argc = 4;
argv[0] = "mycat";
argv[1] = "file1";
argv[2] = "file2";
argv[3] = "file3";

To test this out, use the following program:

#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;
}

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 am confused about where it will be getting files from?
The program will be executed like this:

$ mycat file1 file2 file3

For the purposes of your assignment, you can assume the following for argc and argv of main:

argc = 4;
argv[0] = "mycat";
argv[1] = "file1";
argv[2] = "file2";
argv[3] = "file3";

To test this out, use the following program:

#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;
}

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!

If you have a question with the assignment definition itself, the best person to ask is your professor.

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".

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:

>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.

This article has been dead for over six months. Start a new discussion instead.