Output of C++ programs | Set 34 (File Handling) Last Updated : 15 Sep, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: The program prints "geeksforgeeks" in the file text.txtDescription: 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: 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? 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: 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? 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: content of file before: geeksforgeeks contents of the file after execution: geeksfor geeksDescription: 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. ofile<< " geeks", 6;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: Your initial is gDescription: 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. Comment More infoAdvertise with us Next Article Output of C programs | Set 54 K kartik Improve Article Tags : C++ CPP-Output Practice Tags : CPP Similar Reads Output of C++ programs | Set 44 Output In C++ Q.1 What is the output of below program? CPP #include <iostream> using namespace std; int main() { int x = 0; x = printf("Hello World"); printf(" %d", x); return 0; } Option a) Hello World 10 b) Hello World 11 c) Hello World 12 d) Hello World 0 ans :- b Explan 2 min read Output of C++ programs | Set 45 Q.1 What Is The Output Of this program? CPP #include <iostream> using namespace std; int main() { int a = b = c = 10; a = b = c = 50; printf("%d %d %d", a, b, c); return 0; } Option a) 50 50 50 b) Three Garbage Value c) 10 10 10 d) Compile Time Error Ans: d Explanation : In this prog 2 min read Output of C++ programs | Set 37 Predict the output for the following C++ code: Question 1 CPP #include <iostream> int main() { if (std::cout << "hello") std::cout << " world"; else std::cout << " else part"; return 0; } Output: hello world Description: Since std::cout<< 2 min read Output of C++ programs | Set 35 1. What will be the output of following program? CPP #include<iostream> using namespace std; int main() { int x = 5; if(x==5) { if(x==5) break; cout<<"Hello"; } cout<<"Hi"; } Options A) Compile error B) Hi C) HelloHi D) Hello Answer : A Explanation: Compile erro 3 min read Output of C programs | Set 54 1. What will be the output of the below program? C++ #include <stdio.h> #define GEEKS 100 int main() { #define GEEKS 100 printf("%d", GEEKS); return (0); } Output: - 100 Options: 1. 100 2. compile error 3. No output 4. abnormal termination The answer is option(1). Explanation: As GEE 2 min read Output of C++ programs | Set 50 Predict the output of the following C++ programs:Question 1: CPP#include <cstdlib> #include <iostream> using namespace std; int main() { int ran = rand(); cout << ran << endl; return 0; } Output1804289383 Explanation: As the declared number is an integer, It will produce the 4 min read Like