#include <iostream>
#include <fstream>
#include <string>
int main() {
// Open a file for reading
std::ifstream input_file("input.txt");
// Check if the file was successfully opened
if (!input_file) {
std::cerr << "Error opening input file" << std::endl;
return 1;
}
// Read the contents of the file into a string
std::string input_str;
std::getline(input_file, input_str);
// Close the file
input_file.close();
// Print the contents of the string
std::cout << "The contents of the input file are: " << input_str << std::endl;
// Open a file for writing
std::ofstream output_file("output.txt");
// Check if the file was successfully opened
if (!output_file) {
std::cerr << "Error opening output file" << std::endl;
return 1;
}
// Write the contents of the string to the file
output_file << input_str;
// Close the file
output_file.close();
return 0;
}
This program reads the contents of a file called "input.txt" and stores it in a string. It then writes the contents of the string to a file called "output.txt".
To use this program, you will need to create a file called "input.txt" in the same directory as the program and put some text in it. When you run the program, it will read the contents of the file and write it to "output.txt".
0 Comments