C PROGRAMMING Problem: A simple cat-like program which opens a file whose name i
ID: 3725950 • Letter: C
Question
C PROGRAMMING Problem: A simple cat-like program which opens a file whose name is on the command-line, and copies its contents to stdout. Code:#include <stdio.h>
int main(int argc, char **argv) { FILE *fp; char c;
if (argc < 2) printf("put a file name");
fp = fopen(argv[1], "r+"); while ((c = getc(fp)) > 0) putchar(c); fclose(fp);
return(1); } Submit your improved program in a file named "lab08.c".
Your should not add or change anything which is not specifically to fix a defect in the above program. However, your program should behave like a proper unix program -- for example, one of the defects above is the incorrect exit status. Its error handling and message(s) are also wrong.
Programs which do not compile with "gcc Wall" with no errors or warnings will not get the mark for this lab. However, this is not the only cleanup required. I count ten defects above (although it does depend on how you count), of which you must fix at least six (without introducing any new defects nor changing anything irrelevant) to get the mark. C PROGRAMMING Problem: A simple cat-like program which opens a file whose name is on the command-line, and copies its contents to stdout. Code:
#include <stdio.h>
int main(int argc, char **argv) { FILE *fp; char c;
if (argc < 2) printf("put a file name");
fp = fopen(argv[1], "r+"); while ((c = getc(fp)) > 0) putchar(c); fclose(fp);
return(1); } Submit your improved program in a file named "lab08.c".
Your should not add or change anything which is not specifically to fix a defect in the above program. However, your program should behave like a proper unix program -- for example, one of the defects above is the incorrect exit status. Its error handling and message(s) are also wrong.
Programs which do not compile with "gcc Wall" with no errors or warnings will not get the mark for this lab. However, this is not the only cleanup required. I count ten defects above (although it does depend on how you count), of which you must fix at least six (without introducing any new defects nor changing anything irrelevant) to get the mark. C PROGRAMMING Problem: A simple cat-like program which opens a file whose name is on the command-line, and copies its contents to stdout. Code:
#include <stdio.h>
int main(int argc, char **argv) { FILE *fp; char c;
if (argc < 2) printf("put a file name");
fp = fopen(argv[1], "r+"); while ((c = getc(fp)) > 0) putchar(c); fclose(fp);
return(1); } Submit your improved program in a file named "lab08.c".
Your should not add or change anything which is not specifically to fix a defect in the above program. However, your program should behave like a proper unix program -- for example, one of the defects above is the incorrect exit status. Its error handling and message(s) are also wrong.
Programs which do not compile with "gcc Wall" with no errors or warnings will not get the mark for this lab. However, this is not the only cleanup required. I count ten defects above (although it does depend on how you count), of which you must fix at least six (without introducing any new defects nor changing anything irrelevant) to get the mark.
Explanation / Answer
#include <stdio.h>
int main(int argc, char **argv)
{
FILE *fp;
char c;
argc=2;
if (argc < 2)
printf("Enter file name");
fp = fopen(argv[1], "r+");
if(fp==NULL)
printf("File can't be opened");
while (fp!=NULL && (c = getc(fp)) > 0)
putchar(c);
fclose(fp);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.