从一个文件写入另一个文件,输出错误

编程实践:使用C ++的原理和实践(第二版) 编写一个程序,该程序读取文本文件并将其输入转换为所有小写字母,从而生成一个新文件(目前,我仅专注于生成一个新文件。

//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";

}

所以我有同一程序的两个版本,但是都有问题。第一个打印出此符号-╠,我真的不明白问题出在哪里。第二个仅打印出一个变量-单词本身,它不会从文件中读取。我想第一个版本更好,对吗?以及如何解决该烦人的输出?

ezeke123 回答:从一个文件写入另一个文件,输出错误

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3058780.html

大家都在问