C Programming - File I/O

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Mr_chan
M
Mr_chan
Community Contributor
Quizzes Created: 1 | Total Attempts: 702
| Attempts: 702 | Questions: 12
Please wait...
Question 1 / 12
0 %
0/100
Score 0/100
1. The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":

_A_                                          //preprocessor declaration
main()
{
  char ch;
  _B_                                       //declare the file pointer as: fp
 
  _C_                                       //open the file for writing
  if (fp==_D_){                             //get into the "printf" statement if the file cannot be opened
    printf("Cannot open the file!\n");
    exit(1);
  }

  ch = getchar();                        //get a character from keyboard
  while ( _E_ ){                          //continue processing the input if &ltENTER> is not pressed
    _F_                                     // write 1 character to the file
    _G_                                    // get the next character from keyboard
  }

 _H_                                 // close the file
}

What would be _D_ ?

Explanation

In the given code, _D_ is the file pointer "fp". It is used to open the file for writing. If the file cannot be opened, the condition "fp==NULL" will be true, indicating that the file pointer is pointing to nothing, and the program will enter the "printf" statement to display the message "Cannot open the file!". Therefore, the correct answer for _D_ is NULL.

Submit
Please wait...
About This Quiz
C Programming - File I/O - Quiz

*Please be reminded: when you answer the fill-in-the-blanks questions, do not use excessive/redundant -blanks- ,e. G.
enter:- if(a>b) ,
not if(a > b)
Good luck !

Personalize your quiz and earn a certificate with your name on it!
2. The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":

_A_                                          //preprocessor declaration
main()
{
  char ch;
  _B_                                       //declare the file pointer as: fp
 
  _C_                                       //open the file for writing
  if (fp=_D_){                             //get into the "printf" statement if the file cannot be opened
    printf("Cannot open the file!\n");
    exit(1);
  }

  ch = getchar();                        //get a character from keyboard
  while ( _E_ ){                          //continue processing the input if &ltENTER> is not pressed
    _F_                                     // write 1 character to the file
    _G_                                    // get the next character from keyboard
  }

 _H_                                 // close the file
}

What would be _H_ ?

Explanation

The correct answer for _H_ would be "fclose(fp)". This is because the code is closing the file that was opened for writing using the fopen function. The fclose function is used to close the file and release any resources associated with it.

Submit
3. The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":

_A_                                          //preprocessor declaration
main()
{
  char ch;
  _B_                                       //declare the file pointer as: fp
 
  _C_                                       //open the file for writing
  if (fp==_D_){                             //get into the "printf" statement if the file cannot be opened
    printf("Cannot open the file!\n");
    exit(1);
  }

  ch = getchar();                        //get a character from keyboard
  while ( _E_ ){                          //continue processing the input if &ltENTER> is not pressed
    _F_                                     // write 1 character to the file
    _G_                                    // get the next character from keyboard
  }

 _H_                                 // close the file
}

What would be _B_ ?

Explanation

The variable declaration in C code is done by specifying the data type followed by the variable name. In this case, the variable name is "fp" and the data type is "FILE *", which indicates that "fp" is a pointer to a FILE type. Therefore, the correct answer for _B_ is "FILE *fp;".

Submit
4. The code below read from your keyboard input until an ENTER is pressed, convert any lower case letter to upper case, and then write it to a file:

L1:  ch=getchar();   
L2:  while (ch!='\n') {
L3:      if (ch>='a' and ch<='z')
L4:          ch=ch-'A'+'a';
L5:      fputc(ch,fp);
L6:      ch=getchar();
L7:  }
L8:  fclose(fp);


Which line(s) is/are incorrect ?

Explanation

L3: Use '&&' instead of 'and' for C conditional statement.
L4 should be ch=ch-'a'+'A';
L7: ';' after a loop is optional

Submit
5. The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":

_A_                                          //preprocessor declaration
main()
{
  char ch;
  _B_                                       //declare the file pointer as: fp
 
  _C_                                       //open the file for writing
  if (fp=_D_){                             //get into the "printf" statement if &ltENTER> the file cannot be opened
    printf("Cannot open the file!\n");
    exit(1);
  }

  ch = getchar();                        //get a character from keyboard
  while ( _E_ ){                          //continue processing the input if is not pressed
    _F_                                     // write 1 character to the file
    _G_                                    // get the next character from keyboard
  }

 _H_                                 // close the file
}

What would be _F_ ?

Explanation

The correct answer for _F_ would be "fputc(ch,fp)". This is because the fputc() function is used to write a character to the file specified by the file pointer "fp". In this code, it is used to write the character "ch" to the file.

Submit
6.
 A file (name.txt) containing a list of your classmates reads as below (ALL names follow immediately with a 'new line' character):


What would be the output to the screen on running the code below:

#include &lt;stdio.h&gt;
main()
{
  char str1[20] = "Hello, boys";
  char str2[100] = "God created the world in 7 days.";
  FILE *fp;

  fp=fopen("name.txt","r");
 
  if (fp==NULL){
    printf("Cant open !\n");
    exit(1);
  }
 
  fgets(str1,12,fp);
  fgets(&str2[12],10,fp);

  printf("%s",str2);

  fclose(fp);
}

 

Explanation

The fgets function reads at most one less than the number of characters specified by size from the given stream and stores them in the string str. Reading stops when a newline character is found, at end-of-file or error. The newline, if any, is retained. If any characters are read and there is no error, a '\0' character is appended to end the string.

Submit
7. The correct code of previous question:

L1:  ch=getchar();   
L2:  while (ch!='\n') {
L3:      if (ch>='a' && ch<='z')
L4:          ch=ch-'a'+'A';
L5:      fputc(ch,fp);
L6:      ch=getchar();
L7:  }
L8:  fclose(fp);

Right after execution of L8, a special character would be written automatically to the file (i.e. fp).  (a) What is the character ?   (b) What is the purpose of L8 ?

Explanation

(a) End-of-File
(b) when a file is opened: file data from disk storage is -copied- to the RAM(memory). Memory is important resource for efficient running of the computer.

Submit
8. The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":

_A_                                          //preprocessor declaration
main()
{
  char ch;
  _B_                                       //declare the file pointer as: fp
 
  _C_                                       //open the file for writing
  if (fp=_D_){                             //get into the "printf" statement if the file cannot be opened
    printf("Cannot open the file!\n");
    exit(1);
  }

  ch = getchar();                        //get a character from keyboard
  while ( _E_ ){                          //continue processing the input if &ltENTER> is not pressed
    _F_                                     // write 1 character to the file
    _G_                                    // get the next character from keyboard
  }

 _H_                                 // close the file
}

What would be _G_ ?

Explanation

The correct answer for _G_ would be "fputc(ch, fp)". This is because the code is using the fputc() function to write one character (stored in the variable ch) to the file pointed by the file pointer fp.

Submit
9. The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":

_A_                                          //preprocessor declaration
main()
{
  char ch;
  _B_                                       //declare the file pointer as: fp
 
  _C_                                       //open the file for writing
  if (fp==_D_){                             //get into the "printf" statement if the file cannot be opened
    printf("Cannot open the file!\n");
    exit(1);
  }

  ch = getchar();                        //get a character from keyboard
  while ( _E_ ){                          //continue processing the input if &ltENTER> is not pressed
    _F_                                     // write 1 character to the file
    _G_                                    // get the next character from keyboard
  }

 _H_                                 // close the file
}

What would be _C_ ?

Explanation

_C_ would be "fp=fopen("myfile.txt","w");". This line of code opens the file named "myfile.txt" in write mode and assigns the file pointer to the variable "fp".

Submit
10. The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":

_A_                                          //preprocessor declaration
main()
{
  char ch;
  _B_                                       //declare the file pointer as: fp
 
  _C_                                       //open the file for writing
  if (fp=_D_){                             //get into the "printf" statement if &ltENTER> the file cannot be opened
    printf("Cannot open the file!\n");
    exit(1);
  }

  ch = getchar();                        //get a character from keyboard
  while ( _E_ ){                          //continue processing the input if is not pressed
    _F_                                     // write 1 character to the file
    _G_                                    // get the next character from keyboard
  }

 _H_                                 // close the file
}

What would be _E_ ?

Explanation

The condition for _E_ would be "ch!='\n'", which means that while the character read from the keyboard is not equal to a newline character, the loop will continue processing the input.

Submit
11. The following C code reads from your keyboard (use getchar()) and writes to a file (use fputc() function) named "myfile.txt":

_A_                                          //preprocessor declaration
main()
{
  char ch;
  _B_                                       //declare the file pointer as: fp
 
  _C_                                       //open the file for writing
  if (fp==_D_){                             //get into the "printf" statement if the file cannot be opened
    printf("Cannot open the file!\n");
    exit(1);
  }

  ch = getchar();                        //get a character from keyboard
  while ( _E_ ){                          //continue processing the input if is &ltENTER> not pressed
    _F_                                     // write 1 character to the file
    _G_                                    // get the next character from keyboard
  }

 _H_                                 // close the file
}

What would be _A_ ?

Explanation

not-available-via-ai

Submit
12. The text file "MLKing.txt" consists of the following text:


Determine the output when the following is executed:

#include &ltstdio.h>
main()
{
  int i;
  char c;
  FILE *fptr;
 
  fptr=fopen("MLKing.txt","r");
 
  for(i=0; i<15; i++)
  {
    c=fgetc(fptr);
    if (i>7)
      putchar(c);
  }
 
 fclose(fptr);
}

Explanation

1) at the start of text, i=0
2) the output should be from i=8 to i=14

Submit
View My Results

Quiz Review Timeline (Updated): Feb 20, 2023 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Feb 20, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Mar 16, 2009
    Quiz Created by
    Mr_chan
Cancel
  • All
    All (12)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
The following C code reads from your keyboard (use getchar()) and...
The following C code reads from your keyboard (use getchar()) and...
The following C code reads from your keyboard (use getchar()) and...
The code below read from your keyboard input until an ENTER is...
The following C code reads from your keyboard (use getchar()) and...
 A file (name.txt) containing a list of your classmates reads as...
The correct code of previous question:L1: ...
The following C code reads from your keyboard (use getchar()) and...
The following C code reads from your keyboard (use getchar()) and...
The following C code reads from your keyboard (use getchar()) and...
The following C code reads from your keyboard (use getchar()) and...
The text file "MLKing.txt" consists of the following text:Determine...
Alert!

Advertisement