© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

7. What are the possible ways for file I/o?

Ans:

File I/O can be performed on a character by character basis, a line by line basis, a record by record basis or a chunk by chunk basis.

Ex 1: What is the output?

#include "stdio.h"
{
unsigned char ch;
FILE *fp ;
fp = fopen ( "trial", "r" ) ;
while ( ( ch = getc ( fp ) ) != EOF )
printf ( "%c", ch ) ;
fclose ( fp ) ;
}   

Ans:

Compile time error.

Explanation:
There is no main function.

Ex 2: What is the output?
#include<stdio.h>
main( )
{
FILE *fp ;
char name[25] ;
int age ;
fp = fopen ( "YOURS", "r" ) ;
while ( fscanf ( fp, "%s %d", name, &age ) != NULL )
fclose ( fp ) ;
}

Ans::
 
Infinite loop formed.

Explanation:
fscanf returns the number of input fields successfully scanned, converted, and stored. Here fscanf scanned two fields, so it return 1.

SLogix Student Projects
bottom