473,327 Members | 1,997 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,327 software developers and data experts.

error in file input

hi
i am a novice c++ programmer and am facing a problem in file input
when ever i try to read from file to copy it to other the spaces and
enter is file are missed
the code is as following
#include<fstream>
#include<iostream>
#include<process.h>
using namespace std;
int main(int argc,char *argv[])
{

fstream file1;
fstream file2;
char a;
char fn1[1024],fn2[1024];
cout<<"enter source file ";
cin.get(fn1,1024);
file1.open(fn1,ios::in);
if(file1.fail())
{
cout<<"error file cant be opned\n";
exit(0);
}
else
{
cout<<"file opened succesfully\n";
}
cout<<"enter destination file name";

cin>>fn2;
file2.open(fn2,ios::out);
if(file2.fail())
{
cout<<"error file cant be opned\n";
exit(0);
}
else
{
cout<<" file is opened";
}
while(!file1.eof())
{
file1>>a;
file2<<a;
}

file1.close();
file2.close();
return -1;
}

Sep 26 '05 #1
4 1580
lucifer wrote:
hi
i am a novice c++ programmer and am facing a problem in file input
when ever i try to read from file to copy it to other the spaces and
enter is file are missed
the code is as following
[snip]
while(!file1.eof())
{
file1>>a;
file2<<a;
}

file1.close();
file2.close();
return -1;
}


That's because >> is supposed to miss spaces and newlines.

Try this

char ch;
while (file1.get(ch))
{
file2.put(ch);
}

get() doesn't miss spaces.

john
Sep 26 '05 #2
well the spaces are there but the last char is being repeated again ie
first file contains text umesh is good boy
but the copied file contains umesh is good boyy
help its for my project

Sep 26 '05 #3
lucifer wrote:
well the spaces are there but the last char is being repeated again ie
first file contains text umesh is good boy
but the copied file contains umesh is good boyy
help its for my project


It's a FAQ. You don't read a C++ stream using the

while (!is.eof())
{
}

idiom. eof() checks to see if the *LAST* read failed due to EOF. So
you are trying to read the last char twice.

John gave you the proper idiom -- all istream input returns the stream
for state testing.

while (is.get(ch))
{
// do stuff;
}

or

while (is >> val)
{
}

Sep 26 '05 #4
thank you very much

Sep 26 '05 #5

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

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
4
by: Polly | last post by:
I had a macro that ran a parameter query and created and opened an Excel file with the system date as part of the file name, but I had to change the file name by hand. So I converted the macro to...
0
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) :...
21
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
3
by: dcwbxb | last post by:
Trying to create a program which reads and echos the contents of an input data file, which consists of two records. When I complile my code I keep getting a "error C2001: newline in constant" error....
0
by: pavankumarveera | last post by:
how to unblock the CF below error what was the solution. GES: Potential blocker (pid=282) on resource CF-00000000-00000000; enqueue info in file...
1
by: chrisj | last post by:
I'm using freeASPupload and got some assistance integrating to a Member script. It works successfully. In this modified version there are two groups that use this upload script. Members of one...
10
by: happyse27 | last post by:
Hi All, I got this apache errors(see section A1 and A2 below) when I used a html(see section b below) to activate acctman.pl(see section c below). Section D below is part of the configuration...
1
by: shivasusan | last post by:
Hi Friends! Please Help me! How i can solve this error? Please explain me, what is the error and how to solve? Error Type: ADODB.Recordset (0x800A0E7D) The connection cannot be used to...
4
rahulephp
by: rahulephp | last post by:
i think i am missing something in the below script: It shows error an do not upload files to destination: Let me know how to solve this: <?php if (isset($_POST)) { $uploadArray=...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.