473,394 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Copying one text file to another

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;
}
Jul 22 '05 #1
12 15218
"Gactimus" <ga******@xrs.net> wrote in message
news:1100651820.dZSrLYW+T41iUYlgGChJng@bubbanews.. .
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());

--
ES Kim
Jul 22 '05 #2
"ES Kim" <no@spam.mail> wrote in news:cn**********@news1.kornet.net:
"Gactimus" <ga******@xrs.net> wrote in message
news:1100651820.dZSrLYW+T41iUYlgGChJng@bubbanews.. .
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());


D'oh! It's always the stupid mistakes that get you. Thanks.
Jul 22 '05 #3
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).
"Gactimus" <ga******@xrs.net> wrote in message
news:1100651820.dZSrLYW+T41iUYlgGChJng@bubbanews.. .
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;
}

Jul 22 '05 #4
"Someonekicked" <so***********@comcast.net> wrote in
news:Je********************@comcast.com:
"Gactimus" <ga******@xrs.net> wrote in message
news:1100651820.dZSrLYW+T41iUYlgGChJng@bubbanews.. .
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;
}


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.
Jul 22 '05 #5
Gactimus wrote:
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,

--
Ney André de Mello Zunino
Jul 22 '05 #6
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
Jul 22 '05 #7
Ney André de Mello Zunino <zu****@inf.ufsc.br> wrote in
news:2v*************@uni-berlin.de:
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.


Thanks. I'll try it out.
Jul 22 '05 #8
Ney André de Mello Zunino <zu****@inf.ufsc.br> wrote in news:2v*************@uni-berlin.de:
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 <zu****@inf.ufsc.br> wrote in news:2v*************@uni-berlin.de:
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.
Jul 22 '05 #9

"Gactimus" <ga******@xrs.net> wrote in message
news:7F***********@tornado.tampabay.rr.com...
Ney André de Mello Zunino <zu****@inf.ufsc.br> wrote in
news:2v*************@uni-berlin.de:
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 <zu****@inf.ufsc.br> wrote in
news:2v*************@uni-berlin.de:
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.


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
Jul 22 '05 #10
Gactimus 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.

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.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #11
In article <2v*************@uni-berlin.de>, Ney André de Mello Zunino
<zu****@inf.ufsc.br> writes
Gactimus wrote:
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,


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.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
Jul 22 '05 #12

"Gactimus" <ga******@xrs.net> wrote in message
news:7F***********@tornado.tampabay.rr.com...
| Ney André de Mello Zunino <zu****@inf.ufsc.br> wrote in
news:2v*************@uni-berlin.de:
|
| > 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 <zu****@inf.ufsc.br> wrote in
news:2v*************@uni-berlin.de:
|
| > 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
Jul 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore...
8
by: Randy | last post by:
Hi, is it possible to show the progress of a big file being copied e.g. in a "progressbar"? I tried to use file.copy - but this seems to make no sense :-( Thanks in advance, Randy
10
by: Martin Ho | last post by:
I am running into one really big problem. I wrote a script in vb.net to make a copy of folders and subfolder to another destination: - in 'from.txt' I specify which folders to copy - in...
6
by: Jim Heavey | last post by:
I have written a little application which copies files from one location to another location. Before it copies the file, it ensure the directory for both the from and to locations is valid. It...
3
by: John | last post by:
Hi all, My application updates a sql server 2005 express database prior to copying it with the result being the "in use by another process" and I cannot copy it as a result. I've posted the code...
0
by: berwiki | last post by:
I am trying to copy a table to another SQL 2000 Database, but I continually get errors. When I right-click, choose All-Tasks, Export-Data and go through the DTS settings, I get an 'Unspecified...
6
by: kimiraikkonen | last post by:
Hi, I use system.io.file class to copy files but i have a difficulty about implementing a basic / XP-like progress bar indicator during copying process. My code is this with no progress bar,...
0
by: redAllison21 | last post by:
what i have is a folder that has over 1000 pdf's inside it. I also have a program that runs a querey in SAP and returns a text file with certain pdfs that need to be copied into another folder to be...
13
by: writeson | last post by:
Hi all, I'm writing some code that monitors a directory for the appearance of files from a workflow. When those files appear I write a command file to a device that tells the device how to...
2
by: raylopez99 | last post by:
Beware newbies: I spent a day before I figured this out: copying a bitmap (image) file to file is not quite like copying a text file--you have to do some tricks (see below), like using a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.