Copying one text file to another

G

Gactimus

I wrote a C++ program that is supposed to copy one text file to another.
It works fine if I specify the files to be opened and copied but when I
try to have the file names inputted by the user it doesn't work and I am
stumped. Can anyone clue me in to what I am missing?

Here is my code:


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
string source;
string clone;

cout << "Enter the source file: ";
cin >> source;
cout << "Enter the name of the new file: ";
cin >> clone;

ifstream in(source.c_str); // Open for reading
ofstream out(clone.c_str); // Open for writing
string s;
while(getline(in, s))
out << s << "\n";

return 0;
}
 
E

ES Kim

Gactimus said:
I wrote a C++ program that is supposed to copy one text file to another.
It works fine if I specify the files to be opened and copied but when I
try to have the file names inputted by the user it doesn't work and I am
stumped. Can anyone clue me in to what I am missing?

Here is my code:

ifstream in(source.c_str); // Open for reading
ofstream out(clone.c_str); // Open for writing

ifstream in(source.c_str());
ofstream out(clone.c_str());
 
S

Someonekicked

a faster way would be reading all the file at once ( not looping throughout
lines), maybe it would not be a remarquable difference, anyway you can use
getline(in,s,char(0));
char(0) is the null character, so the delimiter is the null character ( when
reading no more characters).
 
G

Gactimus

a faster way would be reading all the file at once ( not looping
throughout lines), maybe it would not be a remarquable difference,
anyway you can use getline(in,s,char(0));
char(0) is the null character, so the delimiter is the null character (
when reading no more characters).

Thanks for the info. On a side note, do you know how I would go about
encrypting the file that is copied? I want to do it very simply, as in
adding 1 to every ASCII character.
 
G

Gactimus

Ney André de Mello Zunino said:
Of course, you can add '1' or do whatever you like with the character
before you write it to the output file stream.

Ney André de Mello Zunino said:
Of course, you can add '1' or do whatever you like with the character
before you write it to the output file stream.

It put the code in the program but I get an error:

error C2039: 'get' : is not a member of 'basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'

Any ideas? Maybe I just don't know how to implement your code.
 
?

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

Gactimus said:
Thanks for the info. On a side note, do you know how I would go about
encrypting the file that is copied? I want to do it very simply, as in
adding 1 to every ASCII character.

Then read it character by character. Something like (untested):

char c;
while (source.get(c))
clone.put(c);

Hope that helps,
 
?

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

Ney said:
Then read it character by character. Something like (untested):

char c;
while (source.get(c))
clone.put(c);

Of course, you can add '1' or do whatever you like with the character
before you write it to the output file stream.
 
J

John Harrison

Gactimus said:
It put the code in the program but I get an error:

error C2039: 'get' : is not a member of 'basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >'

Any ideas? Maybe I just don't know how to implement your code.

source and clone are not strings, they are the file streams. What you called
in and out in your original code.

Incidentally I would not use the method that Someonekicked recommended, it
does not work if your file contains a null byte. Nor would I use the method
you had originally because that adds an extra newline when the original file
does not end in a newline. Reading and writing one character at a time as
Andre recommended is the simplest.

john
 
K

Karl Heinz Buchegger

Gactimus said:
It put the code in the program but I get an error:

error C2039: 'get' : is not a member of 'basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'

Any ideas? Maybe I just don't know how to implement your code.


Look at the error message. What does it talk about?

It talks about basic_string. Hmm. basic_string. That has something
to do with string! It says that 'get' is not a member of some string
class. But why string? get is something you want to apply to a stream
not a string. So chances are high that you used the wrong variable and
applied get to it. You want to get from the stream and not the string.

I know. Those error messages are cryptic and sometimes even
misleading. But you definitly *must* learn to read them and
draw your conclusions from them.
 
F

Francis Glassborow

Ney André de Mello Zunino said:
Then read it character by character. Something like (untested):

char c;
while (source.get(c))
clone.put(c);

Hope that helps,

Actually there is a subtle problem waiting to bite if you open the files
in text mode; sometimes the encryption mechanism will generate a control
character which will cause problems with reading the encrypted file. It
is, therefore, much safer when encrypting/decrypting a file to open the
file in binary mode.
 
C

Chris \( Val \)

|
| > Ney André de Mello Zunino wrote:
| >
| >> Then read it character by character. Something like (untested):
| >>
| >> char c;
| >> while (source.get(c))
| >> clone.put(c);
| >
| > Of course, you can add '1' or do whatever you like with the character
| > before you write it to the output file stream.
|
|
| > Ney André de Mello Zunino wrote:
| >
| >> Then read it character by character. Something like (untested):
| >>
| >> char c;
| >> while (source.get(c))
| >> clone.put(c);
| >
| > Of course, you can add '1' or do whatever you like with the character
| > before you write it to the output file stream.
|
| It put the code in the program but I get an error:
|
| error C2039: 'get' : is not a member of 'basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >'
|
| Any ideas? Maybe I just don't know how to implement your code.

One method I can think of to copy a file is to open
the file in binary mode, and use the 'rdbuf()' member:

out << in.rdbuf();

Cheers.
Chris Val
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top