将一个文本文件复制到另一个文件 [英] Copying one text file to another

查看:125
本文介绍了将一个文本文件复制到另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个C ++程序,它应该将一个文本文件复制到另一个文件。

如果我指定要打开和复制的文件但是当我支持$ b $时工作正常b尝试让用户输入的文件名不起作用,而且我是b
bumped。谁能让我知道我错过了什么?


这是我的代码:

#include< iostream>

#include< fstream>

#include< cstdlib>

#include< string>

using namespace std;


int main()

{

string source;

string clone;

cout<< 输入源文件:";

cin>>来源;

cout<< 输入新文件的名称:" ;;

cin>>克隆;


ifstream in(source.c_str); //打开阅读

ofstream out(clone.c_str); //开放写作

string s;

while(getline(in,s))

out<< s<< " \ n";


返回0;

}

解决方案

< BLOCKQUOTE>" Gactimus" < GA ****** @ xrs.net>在消息中写道

news:1100651820.dZSrLYW+T41iUYlgGChJng@bubbanews ..。

我写了一个C ++程序,它应该将一个文本文件复制到另一个文本文件。
如果我指定要打开和复制的文件,但是当我试图让用户输入文件名时,它工作正常并且我感到难过。谁能让我知道我错过了什么?

这是我的代码:

ifstream in(source.c_str); //打开阅读
ofstream out(clone.c_str); //打开写入



ifstream in(source.c_str());

ofstream out(clone.c_str()); < br $>

-

ES Kim


" ES Kim" < no@spam.mail>在新闻中写道:cn ********** @ news1.kornet.net:

" Gactimus" < GA ****** @ xrs.net>在消息中写道
新闻:1100651820.dZSrLYW+T41iUYlgGChJng@bubbanews ..

我写了一个C ++程序,应该将一个文本文件复制到
另一个。如果我指定要打开和复制的文件,它可以正常工作
但是当我尝试让用户输入文件名时它不会工作而且我很难过。谁能让我知道我错过了什么?

这是我的代码:

ifstream in(source.c_str); //打开阅读
ofstream out(clone.c_str); //打开写入


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




D''哦!这总是让你愚蠢的错误。谢谢。


一个更快的方法是一次读取所有文件(不循环遍布

行),也许它不会是一个可重新计算的差异,无论如何你可以使用

getline(in,s,char(0));

char(0)是空字符,所以分隔符是空字符(当

不再阅读字符时)。

" Gactimus" < GA ****** @ xrs.net>在消息中写道

news:1100651820.dZSrLYW+T41iUYlgGChJng@bubbanews ..。

我写了一个C ++程序,它应该将一个文本文件复制到另一个文本文件。
如果我指定要打开和复制的文件,但是当我试图让用户输入文件名时,它工作正常并且我感到难过。谁能让我知道我错过了什么?

这是我的代码:

#include< iostream>
#include< fstream>
#include< cstdlib>
#include< string>
使用命名空间std;

int main()
{
字符串来源;
字符串克隆;

cout<< 输入源文件:";
cin>>来源;
cout<< 输入新文件的名称:";
cin>>克隆;

ifstream in(source.c_str); //打开阅读
ofstream out(clone.c_str); //打开写作
字符串s;
while(getline(in,s))
out<< s<< 返回0;
}



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;
}

解决方案

"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


"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.


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;
}



这篇关于将一个文本文件复制到另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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