Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

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

  • Difficulty Level : Medium
  • Last Updated : 29 Jun, 2017

  1. What task does the following program perform?




    #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;
    }

    Answer:

     The program prints "geeksforgeeks" in the file text.txt
    

    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?




       
    #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;
    }

    Answer:

    The program takes input from text.txt file and then prints on the terminal.
    

    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.



  3. What is the Output of following program?




       
    #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;
    }

    Output:

    Reading data from a file:-
    Data in the file:
    /*content of the file */
    

    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.

  4. What will be the changes made to the content of the file after running the code?




       
    #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;
    }

    Output:

    content of file before:
    geeksforgeeks
    
    contents of the file after execution:
    geeksfor geeks
    

    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;
    

    then ofile.seekp(8) will place the pointer at 8th position from thee beginning and then the following will be printed.

    ofile<< " geeks", 6;
    
  5. What is the Output of following program?




       
    #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;
    }

    Output:

    Your initial is g
    

    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. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




My Personal Notes arrow_drop_up
Recommended Articles
Page :