Open In App

Output of C++ programs | Set 34 (File Handling)

Improve
Improve
Like Article
Like
Save
Share
Report
  1. What task does the following program perform? 
     

CPP




#include<iostream>
#include<fstream>
using namespace std;
  
int main() 
{
   ofstream ofile; 
   ofile.open ("text.txt"); 
   ofile << "geeksforgeeks" << endl;
   cout << "Data written to file" << endl;
   ofile.close(); 
   return 0;
}


  1. Answer: 
     
 The program prints "geeksforgeeks" in the file text.txt
  1. Description: When an object is created for ofstream class, it allows us to write into a file just like cout. When opening a file with ofstream object if file is present then the content is erased else it is created. 
     
  2. What task does the following program perform? 
     

CPP




   
#include<iostream>
#include<fstream>
using namespace std;
  
int main() 
{
    char data[100]; 
    ifstream ifile; 
      
    //create a text file before executing.
    ifile.open ("text.txt"); 
    while ( !ifile.eof() ) 
    
        ifile.getline (data, 100); 
        cout << data << endl; 
    }
    ifile.close();
    return 0;
}


  1. Answer: 
     
The program takes input from text.txt file and then prints on the terminal.
  1. Description: When an object is created for ifstream class, it allows us to input from a file just like cin. getline takes the entire line at once.
     
  2. What is the Output of following program? 
     

CPP




   
#include<iostream>
#include<fstream>
#include<string>
#include<cctype>
  
using namespace std;
  
int main() 
{
      
    ifstream ifile; 
    ifile.open ("text.txt"); 
    cout << "Reading data from a file :-" << endl ;
    int c = ifile.peek();
    if ( c == EOF ) return 1;
    if ( isdigit(c) )
    {
        int n;
        ifile >> n;
        cout << "Data in the file: " << n << '\n';
    }
    else
    {
        string str;
        ifile >> str;
        cout << "Data in the file: " << str << '\n';
    }
    ifile.close();
    return 0;
}


  1. Output: 
     
Reading data from a file:-
Data in the file:
/*content of the file */
  1. Description: peek() obtains the next character in the input stream without removing it from that stream. The function accesses the input sequence by first constructing a object. Then, it reads one character from its associated stream buffer object(ifile) by calling its member function sgetc, and finally destroys the object before returning.
     
  2. What will be the changes made to the content of the file after running the code? 
     

CPP




   
#include <iostream>
#include <fstream>     
using namespace std;
  
int main () 
{
    //create a text file named file before running.
    ofstream ofile;
    ofile.open ("file.txt");
      
    ofile<< "geeksforgeeks", 13;
    ofile.seekp (8);
    ofile<< " geeks", 6;
      
    ofile.close();
      
    return 0;
}


  1. Output: 
     
content of file before:
geeksforgeeks

contents of the file after execution:
geeksfor geeks
  1. Description: seekp() is used to move the put pointer to a desired location with respect to a reference point. Using this function the stream pointer is changed to the absolute position (counting from the beginning of the file). In this program the following will write in the file. 
     
ofile<< "geeksforgeeks", 13;
  1. then ofile.seekp(8) will place the pointer at 8th position from the beginning and then the following will be printed. 
     
ofile<< " geeks", 6;
  1. What is the Output of following program? 
     

CPP




   
#include <iostream>     
#include <fstream>
#include <cctype>
  
using namespace std;
  
int main () 
{
    ifstream ifile;
    ifile.open ("text.txt");
      
    //content of file: geeksfor geeks
    char last;
    ifile.ignore (256, ' '); 
    last = ifile.get();     
      
    cout << "Your initial is " << last << '\n';
    ifile.close();
    return 0;
}


  1. Output: 
     
Your initial is g
  1. Description: ignore(256, ‘ ‘) Extracts characters from the input sequence and discards them, until either 256 characters have been extracted, or one compares equal to ‘ ‘.This program prints the first character of the second word in the file. 
     

This article is contributed by I.HARISH KUMAR.

 



Last Updated : 15 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads