Question: For this lab, you will get some practice working with file streams in C. A file stream is a mechanism used to buffer a raw
For this lab, you will get some practice working with file streams in C.
A file stream is a mechanism used to buffer a raw (UNIX) file allowing access to the file in very small blocks, without incurring the enormous overhead/inefficiency of many system level reads and/or writes.
You are given the contents of the file main.c :
#include
#include
int main(int args, char* argv[])
{
// a buffered file pointer
FILE* fin;
// try to open the file input.txt,
// in the current working dir, for read only
fin = fopen("input.txt", "r");
// if open fails - quit
if (!fin)
{
printf(" Can not open input.txt ");
exit(1);
}
else // use the opened file
{
int inchar;
// read chars from buffered file until
// the End Of File marker is found
while ( (inchar = getc(fin)) != EOF )
{
printf("%c ", inchar);
}
// close the opened file
fclose(fin);
}
printf(" ");
return 0;
}
First read through main to understand what it is doing and how.
Compile the program and run it what happens and why?
Fix the problem, and re-run the program do you see what you expect?
Re-write the program so that instead of copying infile.txt to the screen, it creates an outputfile (named output.txt) which is an exact copy of infile.txt
Verify that your program is working
Re-write the program one last time so that it gets the names of the input and output files as command line parameters.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
