0

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?

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
Daemon
  • 1
  • 1
  • 1
    Please don't post links. Include the sources in your question as plain-text. Remember to click the "Code Sample" `{}` formatting option when editing your question. `ios::app` - what do you think it does? `prints out just a variable` - please post _exactly_ what example execution of your programs output on your screen, don't describe it. In the second version you don't read from the file, in the first version the `ifile.get()` fails and you output uninitialized variable Read about [why is while(!feof) wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – KamilCuk Nov 21 '19 at 11:32
  • Your code looks ok. Just instead of reading line by line read it character by charater `char c; in >> c; lower = tolower(c); out << lower;` (tolower is in the std lib) – systemcpro Nov 21 '19 at 11:32
  • `cin.get(str, BUFF);` please explain what this line does. – n. m. could be an AI Nov 21 '19 at 12:06
  • In version 2, `cout << word` is fine and dandy but you are not reading `word` from `ifile`. So you can just remove the code related to `ifile` as it does nothing. – n. m. could be an AI Nov 21 '19 at 12:12
  • And another thing. Right after you are done with a file, close it. This might seem like a minor and unimportant detail, but it really isn't. – n. m. could be an AI Nov 21 '19 at 12:14

0 Answers0