-2

I would like to make a program that reads a specific file, shows it to the user and then makes every letter in that file big (like k->K) using to_upper and shows it to the user again. I don't know what is wrong with my code but it shows some errors.

    using namespace std;
    void to save(){
        fstream plik;
        string napis;
        char z;
        plik.open("name_of_the_file.txt", ios::out | ios::in);
            if(plik.good()){
            cout << "File before using to upper: " << endl;
                while(!plik.eof()) {
                plik >> napis;
                cout << napis << endl;
                }
            cout << "File after using to upper: " << endl;
                while(!plik.eof(z)) {
                    plik.put(toupper(z));
                }
                }
                plik.close();
                }

int main(){

    save();

return (0);
}

I know which libraries should I use I just don't know how to post them here.

Mehdi Mostafavi
  • 880
  • 1
  • 12
  • 25
  • If you get build errors, then copy-paste them as text, in full and complete, into the question body. And add comments on the lines where you get the errors. Please [edit] your question to improve it. Also please take some time to read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Apr 01 '20 at 13:23
  • 1
    `void to save()` is not a valid function declaration – UnholySheep Apr 01 '20 at 13:24
  • And you should take some more time to think through what you're doing. You read the contents of the file once. Then you have a second loop where you don't read anything from the file but attempt to write the *uninitialized* contents of `z` over and over again. – Some programmer dude Apr 01 '20 at 13:25
  • Also please read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Some programmer dude Apr 01 '20 at 13:25

1 Answers1

0

Try this,

#include<iostream>
#include<fstream>
#include<stdlib.h>

using std::cout;
using std::ios;
using std::endl;

void save()
{
    std::fstream plik,temp;
    char z;

    plik.open("my.txt");
    temp.open("temp",std::fstream::trunc|std::fstream::out);

    if(!temp)
    {
        cout<<"File Error";
        exit(1);
    }
    else
    {
        temp.close();
        temp.open("temp");
        if(!temp)
        {
            cout<<"File Error";
            exit(1);
        }
    }

    if(plik.is_open())
    {
    cout << "File before using to upper: " << endl;

    while(plik.get(z))
    {
       cout<<z;
       temp.put(toupper(z));
    }

    cout<<endl;

    plik.clear();
    plik.seekg(0,ios::beg);
    plik.seekp(0,ios::beg);

    temp.clear();
    temp.seekg(0,ios::beg);
    temp.seekp(0,ios::beg);

    while(temp.get(z))
    {
        plik.put(z);
    }

    temp.close();
    remove("temp");

    plik.clear();
    plik.seekg(0,ios::beg);
    plik.seekp(0,ios::beg);

    cout << "File after using to upper: " << endl;

    while(plik.get(z))
    {
       cout<<z;
    }
    }
    else
    {
        cout<<"File Error";
        exit(1);
    }
    plik.close();
}

int main()
{
save();
return (0);
}
srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25