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

07 Feb, 2022
What task does the following program perform?  
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;}
Answer:  
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.  What task does the following program perform?  
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.  
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;}
Answer:  
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. What is the Output of following program?  
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. 
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;}
Output:  
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. What will be the changes made to the content of the file after running the code?  
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. 
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;}
Output:  
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.  
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 the beginning and then the following will be printed.  
then ofile.seekp(8) will place the pointer at 8th position from the beginning and then the following will be printed.  
ofile<< " geeks", 6;
What is the Output of following program?  
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;}
Output:  
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.  
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. 
sumitgumber28
varshagumber28
CPP-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Runtime Errors
Output of C Programs | Set 2
Different ways to copy a string in C/C++
Output of Python Program | Set 1
Output of Java Program | Set 2
Output of Java Program | Set 3
Output of C++ programs | Set 47 (Pointers)
Output of Java Programs | Set 12
Output of Java Program | Set 7
Output of C Programs | Set 3