C ++问题,不显示复制包含在其他文件上。 [英] C++ Question, Doesn't show copying contains on other file.

查看:79
本文介绍了C ++问题,不显示复制包含在其他文件上。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我的程序中有一个问题是将一个文件复制到另一个文件中。它运行退出很好,但问题是它无法显示正确的输出。当文件Name.txt的包含已同步到另一个文件时,它不会显示复制的包含。请告诉我我缺少的东西。

该程序如下:



/ * WAP将一个文件的密码复制到另一个文件中* /



Hello,
I have a problem in my program of copying contains of one file into another file. It runs quit good but the problem is it can't show a proper output. When the contains of file "Name.txt" has coppied to another file then it doesn't shown the copied contains. Please tell me what I am missing.
The Program is follows:

/* WAP to copy containt of one file into another file */

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
ifstream infile;
ofstream outfile;
clrscr();
char infileName[10];
char outfileName[10];
char in_char;

outfile.open("Name.txt",ios::out);
outfile<<"Good\n";
outfile<<"To\n";
outfile<<"See\n";
outfile<<"You\n";
outfile.close();

cout<<"\nEnter file name to copy : ";
cin>>infileName;
infile.open(infileName,ios::in);
if(!infile)
{
cout<<"\nFile "<<infileName <<" not found ";
getch();
exit(0);
}
while(infile.get(in_char))
   cout<<in_char;
   infile.close();
cout<<"\nEnter name of new file in which you want to copy "<<infileName<< " : ";
cin>>outfileName;
outfile.open(outfileName,ios :: out);
if(!outfile)
{
cout<<"\n\aError in opening of "<<outfileName;
getch();
exit(0);
}
cout<<"\n\a\nFile Copy is in progress ......... ";
while(infile.get(in_char))
cout<<"\n\a\nFile Copy Sucessfull ...... ";
cout<<"\n\nNew File is : "<<outfileName;
cout<<in_char;
infile.close();
outfile.close();
getch();
}



输出:

输入要复制的文件名:Name.txt

好​​



请参阅





输入你想要的新文件的名称复制Name.txt:Read.txt



文件复制正在进行中.......



文件复制Sucessfull .....



新文件是:Read.txt





现在,它应该打印Read.txt的包含但不能打印它们。我试过但没有得到任何东西,所以请给我一些提示。

谢谢......




Output:
Enter file name to copy : Name.txt
Good
To
See
You

Enter name of new file in which you want to copy Name.txt : Read.txt

File Copy is in progress.......

File Copy Sucessfull.....

New File is : Read.txt


Now, here it should prints the contains of Read.txt but it can't prints them. I tried but didn't get anything, So please give me some hint.
Thanks...

推荐答案

尝试执行复制操作时, infile 已经关闭。您必须在复制前重新打开输入文件或不关闭它(在这种情况下回放流)。



您也没有执行任何写入操作输出文件。似乎在第二个读取行之后缺少此操作

When trying to perform the copy operation, your infile has already been closed. You must re-open the input file before copying or don't close it (rewind the stream in this case).

You are also not performing any write operation to the output file. It seems that this operation is missing after the second read line
while(infile.get(in_char))


这篇关于C ++问题,不显示复制包含在其他文件上。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆