An exercise from Programming: Principles and Practice Using C++ (2nd Edition) Write a program that reads a text file and converts its input to all lower case, producing a new file(for now I concentrated on producing a new file solely.
//version one
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int BUFF = 256;
int main() {
char str[BUFF];
char ch;
ofstream ofile("example.txt");
if (ofile.is_open())
{
cout << "enter sth:\n";
cin.get(str, BUFF);
ofile << str;
}
else cout << "Unable to open file";
ifstream ifile(str);
ifile.open("new.txt", ios::app);
if (ifile.is_open())
{
cout << "here is what u got:\n";
while (!ifile.eof())
{
ifile.get(ch);
cout << ch;
}
}
else cout << "Unable to open file";
}
//version two
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string word;
ofstream ofile;
ofile.open("example.txt");
if (ofile.is_open())
{
cout << "enter sth:\n";
getline(cin, word);
ofile << "it was passed+" << word;
}
else cout << "Unable to open file";
ifstream ifile;
ifile.open("new.txt");
if (ifile.is_open())
{
cout << "here is what u got:\n";
cout << word;
}
else cout << "Unable to open file";
}
So I have two versions of the same program, but both have issues. The first one prints out this symbol - ╠ and I really cannot get what is the problem. And the second one prints out just a variable - word itself, it doesn't read from a file. I suppose the first version is better, right? And how to fix that annoying output?