Files and Streams
- What is the output of this program?
#include <iostream>
int main ()
{
FILE * ptr;
long size;
ptr = fopen ("Example.txt", "rb");
if (ptr == NULL)
perror ("Error opening file");
else
{
fseek (ptr, 0, SEEK_END);
size = ftell (ptr);
fclose (ptr);
printf (" %ld\n", size);
}
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
This program will return the size of the file in bytes.
- What is the output of this program in the file?
#include <iostream>
int main ()
{
freopen ("Sample.txt", "w", stdout);
printf ("This is redirected to a file");
fclose (stdout);
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
In this program, We are sending the text to the file by opening the file using the function freopen.
- What is the output of this program?
#include <iostream>
int main ()
{
int k;
FILE * ptr;
char buff[10];
ptr = fopen ("Sample.txt", "w+");
for ( k = 'A' ; k <= 'D' ; k++)
fputc ( k, ptr);
rewind (ptr);
fread (buff, 1, 5, ptr);
fclose (ptr);
buff[3] = '\0';
puts (buff);
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
In this program, We are setting the buffer size upto 3 only, So it is printing ABC.
- What is the output of this program in the text file?
#include
int main ()
{
FILE * ptr;
char buff[] = { 'I' , 'L' , 'U' };
ptr = fopen ( "Example.txt" , "wb" );
fwrite (buff , 1 , sizeof(buff) , ptr );
fclose (ptr);
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
In this program, We are writing into the file by using the function fwrite.
- By using which function does the buffer are automatically flushed?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
fclose