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

C and C++ Interview Questions

5. What are all the file opening modes in c?

Ans:

These are
1. "r" ->It open the file in read mode.It points the first character in the file
2."w" ->If the file exists,its contents are overwritten.If the file doesn't exist, a new file is created.Retruns Null, if unable to open.
3. "a"->If file exists it open the file and set the pointer to the last character in it.If the file is doesn't exist, a new file is created.

4."r+" ->Searches file. If is opened successfully then loads it into memory and sets up a pointer which points to the first character in it. Returns NULL, if unable to open the file.

5."w+" ->Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist a new file is created. Returns NULL, if unable to open file.

6."a+"-> Searches file. If the file is opened successfully then loads it into memory and sets up a pointer which points to the first character in it. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file.

Ex 1: Is this program can compile and run?

#include "stdio.h"
 main( )
{
FILE *fp ;
openfile ( "Myfile.txt", fp ) ;
if ( fp == NULL )
printf ( "Unable to open file…" ) ;
}
openfile ( char *fn, FILE **f )
{
*f = fopen ( fn, "r" ) ;
}

Ans:
Compile and run successfully

Ex 2: What is the output?

main( )
{
char fname[ ] = "c:\\students.dat" ;
FILE *fp ;
fp = fopen ( fname, "tr" ) ;
if ( fp == NULL )
printf ( "\nUnable to open file..." ) ;
}

Output:

Compiler error.

Explanation:

FILE structure available in stdio.h header. Here this program to not include the stdio.h.so compiler generate the error.

SLogix Student Projects
bottom