Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a section of code that attempts to open file foo.txt for input and, if

ID: 3677335 • Letter: 1

Question

1. Write a section of code that attempts to open file foo.txt for input and, if the file does not exist, generates an error message and aborts the program
2. What could happen if you attempt to open a file for input and you do not check to see where or not the file exists.
3. Write a section of code that opens file diary.txt to add new data to the file without modifying the data that might already be in the file.
4. What could happen if you fail to close a file properly using the fclose() function.
5. Assume that the data file, data.txt, contains the data for a checking account. Each line in the data file contains a character for deposit or check cashed ('D' or 'C'), and a amount that is represented as a double. Write a section of code that opens the data.txt input file, reads the data on each line in the file, and outputs the data to the terminal window.
6. Repeat the following exercise but this time output each line of data to a new output file, checking.txt.

Explanation / Answer

1. Section of Code:

   //File Pointer
   FILE *fp;

   //Opening a file
   fp = fopen("foo.txt", "r");
  
   //Condition that checks for file existence
   //Same condition is used for file existence and also for file permissions
   if(fp == NULL)
   {
       //Error Message
       exit(0);
   }
   else
   {
       //Remaining Code
   }

---------------------------------------------------------------------------------------------------------------------------------------------  
  
2.

If the file was opened with out checking whether file exists or not, the program gets terminated along with an error message while using the pointer that points to the file.

---------------------------------------------------------------------------------------------------------------------------------------------

3. Section of Code:

   //File Pointer
   FILE *fp;

   //Opening a file in append mode for adding new data without modifying old data
   fp = fopen("dairy.txt", "a");
  
   //Remaining Code

--------------------------------------------------------------------------------------------------------------------------------------------

4.

The "fclose" function with the file pointer in the parentheses closes the file. However the system itself will close all open files before exiting from the program but it is good programming practice of closing all files once there is of no use with them.

If the file was not closed properly there is a chance of having inconsistent data on the file.

--------------------------------------------------------------------------------------------------------------------------------------------

5.   Section of Code:

   FILE *fp;
char ch;
double amount;

fp = fopen("data.txt","r");

//If file doesn't exist
if(fp == NULL)
{
//Error Message
exit(0);
}
else
{
//File Exists

//Read data from file and print to console
do
{
fscanf(fp,"%c %f", ch, amount);
printf("%c %f", ch, amount);
}while(!feof(fp));
}

//Close the file
fclose(fp);

------------------------------------------------------------------------------------------------------------------------------------  

6.   Section of Code:

   FILE *fpread, *fpwrite;
char ch;
double amount;

//Open file for reading
fpread = fopen("data.txt","r");

//Open file for writing data
fpwrite = fopen("checking.txt","w");

//If file doesn't exist
if(fpread == NULL)
{
//Error Message
exit(0);
}
else
{
//File Exists

//Read data from file and print to console
do
{
//Reading data from file
fscanf(fpread, "%c %f", ch, amount);

//Writing data to a file
fprintf(fpwrite, "%c %f", ch, amount);
}while(!feof(fpread));
}

//Close the files
fclose(fpread);
fclose(fpwrite);