0

I have this function to copy a file word for word to another file.

void copy()
    {
        ifstream file;
        file.open("file");
        ofstream tabel;
        tabel.open("copy");
        string word;
        char x;
        x = file.get();
        while (x != ' ')
        {
            word += x;
            x = file.get();

        }
        tabel << word;
        word.clear();
        while (!file.eof())
        {
            x = file.get();
            while (x != ' ')
            {
                word += x;
                x = file.get();
            }
            tabel << word << " ";
            word.clear();
        }
    }

But my VS gets stuck when I Run this code. Why I cant write to "tabel" file?

I think the problem is here

            while (!file.eof())
            {
                x = file.get();
                while (x != ' ')
                {
                    word += x;
                    x = file.get();
                }
                tabel << word << " ";
                word.clear();
            }
dodekja
  • 403
  • 6
  • 16
  • 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 Dec 28 '20 at 12:18
  • 1
    On another note, when you read strings using the "input" operator `>>` then it will read *space delimited* words. So what you could do is simple `while (file >> word) { tabel << word << ' '; }`. If you need to keep the line structure then use [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline) to read full lines into a string, put that string into a [`std::istringstream`](https://en.cppreference.com/w/cpp/io/basic_istringstream) that you then read words from. – Some programmer dude Dec 28 '20 at 12:21
  • 2
    Then finally about the problem with your current code, please read more about [`std::istream::get`](https://en.cppreference.com/w/cpp/io/basic_istream/get). Pay close attention to what it ***returns***, especially in the case of end of file. The inner loop `while (x != ' ')` will never end if you reach end of file. – Some programmer dude Dec 28 '20 at 12:24
  • 1
    I wonder though, wouln't [getline](http://www.cplusplus.com/reference/string/string/getline/) be better at copying file, considering all pointed out above – Eugene Anisiutkin Dec 28 '20 at 12:25

2 Answers2

1

Maybe this might be helpful:

#include <fstream>

int main() {
    std::ifstream in("file");
    std::ofstream out("copy");

    std::string word;
    while (getline(in, word, ' ')) {
        out << word;
    }
}
adembudak
  • 1,060
  • 12
  • 22
  • 2
    The code in the question would (if it worked) skip consecutive spaces, so it's not a straight up copy. And it might be part of a school exercise or assignment to learn how to handle streams. And C++17 unfortunately isn't that widespread yet, especially not at schools it seems. – Some programmer dude Dec 28 '20 at 12:34
  • 1
    Yeah, I just jumped in when I saw the copy :) – adembudak Dec 28 '20 at 13:01
1

Here is my answer:
Before running this code you must create file.txt in the project folder. The code is commented.

void copy()
        {
            std::fstream file("file.txt");//open file
            
            std::ofstream tabel("copy.txt");//open tabel
        
            std::string word = "";
        
            while (file)
            {
                file >> word;//read until space(word by word)
                tabel << (word + " ");//add word to tabel
                word = "";
            }
            //closing files
            file.close();
            tabel.close();
        }
aboloo
  • 79
  • 4