How do i read in a file and move it to two other locations?

I want to copy a file to a new file. I thought this would do it but it doesn't copy it over to the new one

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 #include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

int main () {
  string line;
  ifstream myfile ("myfile.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,text) )
    {
      cout << text << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file";

  ofstream file ("newfile.txt");
  if (file.is_open())
  {
    file << line;
    file.close();
  }


  return 0;
}
Last edited on
You're attempting to write the "line" out to the new file you open.
file << line;

But you never give line the information you read from the original file.
closed account (28poGNh0)
// First text is undeclard

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

// First text is undeclard

int main ()
{
    string dataFile;

    ifstream myfile ("test.txt"); 
    if (myfile.is_open())
    {
        while (myfile)
        {
            char ch = myfile.get();
            cout << ch;
            if(ch!=EOF)
                dataFile += ch; 
        }
        myfile.close();
    }
    else
    {
        cout << "Unable to open file";
        exit(-1);//Because I think You dont want to write in file with no data
    }

    ofstream file ("newfile.txt");
    if (file.is_open())
    {
        file << dataFile;
        file.close();
    }

    return 0;
}
Last edited on
Oops i pasted the wrong thing. Why doesn't this work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

int main () {
  string line;
  ifstream myfile ("myfile.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file";

  ofstream file ("newfile.txt");
  if (file.is_open())
  {
    file << line;
    file.close();
  }


  return 0;
}



You're reading through the original file line by line, then you write out the last line you read to the new file. You need to store each string you read in, then write out those strings in a loop to your new file.

An example using a std::queue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
#include <string>
#include <queue>

int main()
{
	std::queue<std::string> fileContents;
	std::string line;
	
	std::ifstream myfile ("original.txt");
	if (myfile.is_open())
	{
		while ( getline (myfile,line) )
		{
			fileContents.push(line);
		}
		myfile.close();
	}

	else std::cout << "Unable to open file";
	
	std::ofstream file ("newfile.txt");
	if (file.is_open())
	{
		while(fileContents.size() != 0)
		{
			file << fileContents.front() << "\n";
			fileContents.pop();
		}
		file.close();
	}
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>

int main()
{
    const char* const path_to_srce_file = "myfile.txt" ;
    const char* const path_to_dest_file = "newfile.txt" ;

    // open the source file for text input
    std::ifstream in( path_to_srce_file ) ;

    // open the destination file for text output,
    // truncate it if it exists, create it if it doesn't
    std::ofstream out( path_to_dest_file ) ;

    // copy the contents of the source file to the destination file
    out << in.rdbuf() ;
}
Topic archived. No new replies allowed.