472,978 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Copying entire file, line by line into another file

Hi, I've written the following code to copy one files contents line by
line into another file. However I'm a bit dubious about using the
infinite loop to do this.

ifstream infile("File1.txt");
ofstream outfile("File2.txt");
string line = "";

while (true)
{
getline(infile,line);
//Process and edit line
outfile << line;
if (!infile.good())
break;
outfile << '\n';
}

The reason I'm not just doing

while (!infile.eof())
{
getline(infile,line);
outfile << line << '\n';
}

or

while (getline(infile,line))
{
outfile << line << '\n';
}

Is because it results in having an extra '\n' at the end of my outout
file. Perhaps you guys could let me know the correct way of achieving
what I'm trying to do.

Aug 5 '06 #1
8 10326
Jared Wiltshire wrote:
Hi, I've written the following code to copy one files contents line by
line into another file. However I'm a bit dubious about using the
infinite loop to do this.

ifstream infile("File1.txt");
ofstream outfile("File2.txt");
string line = "";

while (true)
{
getline(infile,line);
//Process and edit line
outfile << line;
if (!infile.good())
break;
outfile << '\n';
}

The reason I'm not just doing

while (!infile.eof())
{
getline(infile,line);
outfile << line << '\n';
}

or

while (getline(infile,line))
{
outfile << line << '\n';
}

Is because it results in having an extra '\n' at the end of my outout
file. Perhaps you guys could let me know the correct way of achieving
what I'm trying to do.
I think to copy a file I would try something along these lines:

#include <fstream>

void copy_file(const std::string & in, const std::string & out)
{
std::ifstream in_file(in.c_str(), std::ios::binary) ;
std::ofstream out_file(out.c_str(), std::ios::binary) ;

out_file << in_file.rdbuf() ;
}

--
Alan Johnson
Aug 5 '06 #2
Alan Johnson wrote:
Jared Wiltshire wrote:
Hi, I've written the following code to copy one files contents line by
line into another file. However I'm a bit dubious about using the
infinite loop to do this.

ifstream infile("File1.txt");
ofstream outfile("File2.txt");
string line = "";

while (true)
{
getline(infile,line);
//Process and edit line
outfile << line;
if (!infile.good())
break;
outfile << '\n';
}

The reason I'm not just doing

while (!infile.eof())
{
getline(infile,line);
outfile << line << '\n';
}

or

while (getline(infile,line))
{
outfile << line << '\n';
}

Is because it results in having an extra '\n' at the end of my outout
file. Perhaps you guys could let me know the correct way of achieving
what I'm trying to do.

I think to copy a file I would try something along these lines:

#include <fstream>

void copy_file(const std::string & in, const std::string & out)
{
std::ifstream in_file(in.c_str(), std::ios::binary) ;
std::ofstream out_file(out.c_str(), std::ios::binary) ;

out_file << in_file.rdbuf() ;
}

--
Alan Johnson
Thanks, but I'm trying to copy the file line by line. The reason for
this is because I need to edit certain lines before I write them to the
new file.

Aug 5 '06 #3
Jared Wiltshire wrote:
Hi, I've written the following code to copy one files contents line by
line into another file. However I'm a bit dubious about using the
infinite loop to do this.

ifstream infile("File1.txt");
ofstream outfile("File2.txt");
string line = "";
The reason I'm not just doing

while (!infile.eof())
{
getline(infile,line);
outfile << line << '\n';
}
This will output an extra line.
or

while (getline(infile,line))
{
outfile << line << '\n';
}
This will not.

--
Ian Collins.
Aug 5 '06 #4
hi,
you can use this code:

while (!infile.eof())
{
getline(infile,line);
outfile << line;
if(!infile.eof())
outfile << '\n';
}

Or

while (!infile.eof())
{
getline(infile,line);
outfile << line << ( infile.eof() ? '' : '\n' );
}

Ian Collins wrote:
Jared Wiltshire wrote:
Hi, I've written the following code to copy one files contents line by
line into another file. However I'm a bit dubious about using the
infinite loop to do this.

ifstream infile("File1.txt");
ofstream outfile("File2.txt");
string line = "";
The reason I'm not just doing

while (!infile.eof())
{
getline(infile,line);
outfile << line << '\n';
}
This will output an extra line.
or

while (getline(infile,line))
{
outfile << line << '\n';
}
This will not.

--
Ian Collins.
Aug 5 '06 #5
keyvan wrote:
hi,
you can use this code:

while (!infile.eof())
{
getline(infile,line);
outfile << line;
if(!infile.eof())
outfile << '\n';
}

Or

while (!infile.eof())
{
getline(infile,line);
outfile << line << ( infile.eof() ? '' : '\n' );
}
1. Don't top-post.

2. You code is incorrect. See FAQ 15.5
(http://www.parashift.com/c++-faq-lit....html#faq-15.5) for
details.
Aug 5 '06 #6
Ian Collins wrote:
Jared Wiltshire wrote:
Hi, I've written the following code to copy one files contents line by
line into another file. However I'm a bit dubious about using the
infinite loop to do this.

ifstream infile("File1.txt");
ofstream outfile("File2.txt");
string line = "";
The reason I'm not just doing

while (!infile.eof())
{
getline(infile,line);
outfile << line << '\n';
}
This will output an extra line.
or

while (getline(infile,line))
{
outfile << line << '\n';
}
This will not.

--
Ian Collins.
Try it when the input has a character return/line feed at the end of
the last line.

Aug 7 '06 #7
keyvan wrote:
hi,
you can use this code:

while (!infile.eof())
{
getline(infile,line);
outfile << line;
if(!infile.eof())
outfile << '\n';
}

Or

while (!infile.eof())
{
getline(infile,line);
outfile << line << ( infile.eof() ? '' : '\n' );
}

Ian Collins wrote:
Jared Wiltshire wrote:
Hi, I've written the following code to copy one files contents line by
line into another file. However I'm a bit dubious about using the
infinite loop to do this.
>
ifstream infile("File1.txt");
ofstream outfile("File2.txt");
string line = "";
>
>
The reason I'm not just doing
>
while (!infile.eof())
{
getline(infile,line);
outfile << line << '\n';
}
>
This will output an extra line.
or
>
while (getline(infile,line))
{
outfile << line << '\n';
}
>
This will not.

--
Ian Collins.
I was trying to avoid multiple conditional statements. I'll probally
just end up using the form that Ian Collins referred to. The file
probally should end in a character return/line feed anyway.

By the way if its rude to "double post" let me know, I dont have much
idea about newsgroups (I'm using Google Groups).

Aug 7 '06 #8
Jared Wiltshire wrote:
Ian Collins wrote:
>>Jared Wiltshire wrote:
>>>
while (getline(infile,line))
{
outfile << line << '\n';
}

This will not.

Try it when the input has a character return/line feed at the end of
the last line.
Please trim signatures in your responses.

getline strips the line terminator.

--
Ian Collins.
Aug 7 '06 #9

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...
3
by: Son KwonNam | last post by:
Hello, When there a two forms like the follosings, <form name="source"> <input name="a..." value=".." ..> <input name="a..." value=".." ..> <input name="a..." value=".." ..> <input...
4
by: zMisc | last post by:
Is it possible to copy a table from one schema to another schema by just copying the frm file to the directory for the new schema? What is the best way to create a new database with all the...
17
by: Michael | last post by:
Hello, I am writing an app that will sit on the desktop. It needs to be able to make a copy of a folder that sits on the server. It will copy the folder from the server and place it in the same...
15
by: Shuch | last post by:
Hi all, i m trying to read from a file and then copy it into an array...my code is as follow..it runs fine but i cant understand y it doesnt show me any output?? here is my code... using...
5
by: ozzii | last post by:
Hi I am using the following code to copy data from one database table into another database table: SELECT * INTO Products From exportdb.mdb.exporttable However the query simply deletes...
2
by: Mike | last post by:
I am trying to write a little program for my own use using VB2005 express edition. I have a list of peoples names in a file that I read into an array of strings. I am using a multiline textbox to...
18
by: mike3 | last post by:
Hi. I have an interesting problem. The C program presented below takes around 12 seconds to copy 128 MB of data on my machine. Yet I know the machine can go faster since a copying done at a...
2
by: cckramer | last post by:
When I read file A , it has these lines like that need to be reproduced: Input a, b, c, X,Y;
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.