c ++转换文件 [英] c++ conversion files

查看:63
本文介绍了c ++转换文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我想创建一个文件,将文本文件从

unix转换为windows和windows到unix

我理解它的一般概念为

unix使用换行LF

Windows使用CRLF carrige返回和馈送线

我的程序会提示用户输入要打开的文件然后

提示输入一个目标文件以保存新格式化的文件。

我几乎没有遇到任何问题,我一直试图解决几个问题

小时

这里是我创建的代码

#include< iostream>

#include< fstream>

using namespace std;


const string unix =" / uw" ;;

const string windows =" / wu" ;;

const string help =" /?" ;;

int uw();

int wu();

int helpfile( );


int main()

{

字符串选项;

cout<< ; " \ n";

cout<< ************************菜单********************* \ n" ;;

cout<< " \ n";

cout<< "请从以下列表中选择\ n" ;;

cout<< " \ n";

cout<< " *输入/ uw将文件从Unix转换为Windows \ n" ;;

cout<< " *输入/ wu将文件从Windows转换为Unix \ n" ;;

cout<< " *键入帮助以显示帮助文件\ n" ;;

getline(cin,option);

while((选项!= unix)&&(选项!= windows)&&(选项!=帮助)){

cout<< "这不是一个有效的选择!再试一次'\\ nn ;;

getline(cin,option);

}


if(option == unix) {

uw();

}否则如果(选项==窗口){

wu();

} else if(option == help){

helpfile();

}


返回0;

}


int uw(){

char FileName [20];

char目的地[20] ;

cout<< 请输入源文件的名称:\ n";

cin>> FileName;


ifstream in(FileName,ios :: binary | ios :: in);

if(!in){

cout<< 错误打开源\ n;

返回0;

}


cout<< 请输入目标文件的名称:\ n";

cin>>目的地;

while(FileName == Destination){

cout<< 源和目标文件名必须不同请

尝试另一个:\ n" ;;

cin>>目的地;

}

ofstream out(目的地,ios :: binary | ios :: out);

if(!out){

cout<< 创建目标文件时出错\ n;

返回0;

}

char c;


while(in.get(c)){

out.put(c);

if(c == 13){

cout<< 回车,不是Unix文本文件,请重新考虑

选项\ n;;

}

if(c == 10) {

cout<< 换行\ n;

}

}


in.close();

out.close();


返回0;

}



int wu(){

char FileName [20];

char目的地[20];

cout<< 请输入源文件的名称(即)file1.txt

\ n" ;;

cin>> FileName;

ifstream in(FileName,ios :: binary | ios :: in);


if(!in){

cout<< 错误打开源\ n;

返回1;

}

cout<< 请输入目标文件的名称:\ n";

cin>>目的地;


while(FileName == Destination){

cout<< 源和目标文件名必须不同请

尝试另一个:\ n" ;;

cin>>目的地;

}

ofstream out(目的地,ios :: binary | ios :: out);

if(!out){

cout<< 创建目标文件时出错\ n;

返回0;

}


char c;


while(in.get(c)){

out.put(c);

if(c == 13){

cout<< Carriage return\\\
;

}

if(c == 10){

cout<< 换行\ n;

}

}


in.close();

out.close();


返回0;

}


int helpfile(){


//系统(暂停);

cout<< \ n \ n \\ nHere是c:\\help.txt \ n \ n" ;;

ifstream inf(" c:\\ help.txt", ios :: in);

if(!inf){

cout<< 读取文件的错误\ n;

返回1;

}

string theLine ="" ;;

while(getline(inf,theLine)){

cout<< theLine<<结束;

}

inf.close();


返回0;


}

Hi everyone I am trying to create a file that converts text files from
unix to windows and windows to unix
I understand the general concept of it as
unix uses line feed LF
Windows uses CRLF carrige return and feed line
my program will prompt the user to enter a file to open and then
prompts for a destination file to save the new formatted file.
I am having few problems that i have been trying to solve for few
hours
here is the code I created
#include <iostream>
#include <fstream>
using namespace std;

const string unix = "/uw";
const string windows = "/wu";
const string help = "/?";
int uw();
int wu();
int helpfile();

int main()
{
string option;
cout << "\n";
cout << "************************ Menu *********************\n";
cout << "\n";
cout << " Please choose from the following list\n";
cout << "\n";
cout << " * type /uw to convert a file from Unix to Windows\n";
cout << " * type /wu to convert a file from Windows to Unix\n";
cout << " * type Help to Display the help file\n";
getline(cin,option);
while((option != unix) && (option!= windows) && (option != help) ){
cout << " that''s not a valid option! Try again\n";
getline(cin,option);
}

if (option == unix){
uw();
}else if(option == windows){
wu();
}else if(option == help){
helpfile();
}

return 0;
}

int uw(){
char FileName[20];
char Destination[20];
cout << "please enter the name of the source file: \n";
cin >> FileName;

ifstream in(FileName,ios::binary | ios:: in);
if(!in){
cout << "Error opening source\n";
return 0;
}

cout << "please enter the name of the destination file: \n";
cin >> Destination;
while(FileName == Destination){
cout << "Source and Destination file names must be different please
try another:\n";
cin >> Destination;
}
ofstream out(Destination,ios::binary | ios::out);
if(!out){
cout << "Error creating destination file \n";
return 0;
}
char c;

while(in.get(c)){
out.put(c);
if(c == 13){
cout << "Carriage return, not a Unix text file please reconsider
option\n";
}
if(c == 10){
cout << "Line feed\n";
}
}

in.close();
out.close();

return 0;
}


int wu(){
char FileName[20];
char Destination[20];
cout << "please enter the name of the source file (i.e.) file1.txt
\n";
cin >> FileName;
ifstream in(FileName ,ios::binary | ios:: in);

if(!in){
cout << "Error opening source\n";
return 1;
}
cout << "please enter the name of the destination file: \n";
cin >> Destination;

while(FileName == Destination){
cout << "Source and Destination file names must be different please
try another:\n";
cin >> Destination;
}
ofstream out(Destination,ios::binary | ios::out);
if(!out){
cout << "Error creating destination file \n";
return 0;
}

char c;

while(in.get(c)){
out.put(c);
if(c == 13){
cout << "Carriage return\n";
}
if(c == 10){
cout << "Line feed\n";
}
}

in.close();
out.close();

return 0;
}

int helpfile(){

//system("pause");
cout << "\n\nHere is c:\\help.txt \n\n";
ifstream inf("c:\\help.txt",ios::in);
if(!inf){
cout << "Error reading file\n";
return 1;
}
string theLine = "";
while(getline(inf,theLine)){
cout << theLine << endl;
}
inf.close();

return 0;

}

推荐答案

2004年12月3日20:56:23 -0800, ka ***** @ hotmail.com (kalio80)写道:
On 3 Dec 2004 20:56:23 -0800, ka*****@hotmail.com (kalio80) wrote:
大家好我正在尝试创建一个转换文本文件的文件
unix到windows和windows unix
我理解它的一般概念为
unix使用换行LF
Windows使用CRLF carrige返回和馈线
我的程序将提示用户输入要打开的文件,然后提示输入目标文件以保存新格式化的文件。
我几乎没有遇到任何问题,我一直试图解决几个问题
小时
Hi everyone I am trying to create a file that converts text files from
unix to windows and windows to unix
I understand the general concept of it as
unix uses line feed LF
Windows uses CRLF carrige return and feed line
my program will prompt the user to enter a file to open and then
prompts for a destination file to save the new formatted file.
I am having few problems that i have been trying to solve for few
hours




我只是在学习C ++,我一般都使用Perl,这将是一个简单的1-liner



但是为了我的学习,我试图让你的脚本运行,并且

belo w是一个工作版本,但是我删除了你在菜单中使用的麻烦的字符串

,然后去了int'。所以你需要在你的琴弦上工作



#include< iostream>

#include< fstream>

使用命名空间std;


#define CR 0x0d

#define LF 0x0a


int uw();

int wu();

int helpfile();


int main()

{

int option;

cout<< " \ n";

cout<< ************************菜单

*************** ****** \ n" ;;

cout<< " \ n";

cout<< "请从以下列表中选择\ n" ;;

cout<< " \ n";

cout<< " *输入1将文件从Unix转换为Windows \ n" ;;

cout<< " *键入2将文件从Windows转换为Unix \ n;

cout<< " *输入3以显示帮助文件\ n" ;;


cin>>选项;


while((选项!= 1)&&(选项!= 2)&&(选项!= 3))

{

cout<< "这不是一个有效的选择!再试一次'\\ n';


cin>>选项;


}

if(选项== 1)

{

uw() ;

}

否则if(选项== 2)

{

wu();

}

else if(option == 3)

{

helpfile();

}

返回0;

}

int uw()

{

char FileName [20];

char目的地[20];

cout<< 请输入源文件的名称:\ n";

cin>> FileName;

ifstream in(FileName,ios :: binary | ios :: in);

if(!in)

{

cout<< 错误打开源\ n;

返回0;

}

cout<< 请输入目标文件的名称:\ n";

cin>>目的地;

while(FileName == Destination)

{

cout<< 源和目标文件名必须不同

请尝试另一个:\ n" ;;

cin>>目的地;

}

ofstream out(目的地,ios :: binary | ios :: out);

if(!out)

{

cout<< 创建目标文件时出错\ n;

返回0;

}

char c;

while(in.get(c))

{

if(c == LF)

{

out.put(CR);

out.put(LF);

}

其他

{

out.put(c);

}


}

in.close();

out.close();

返回0;

}


int wu()

{

char FileName [20];

char目的地[20];

cout<< 请输入源文件的名称(即)

file1.txt \ n" ;;

cin>> FileName;

ifstream in(FileName,ios :: binary | ios :: in);

if(!in)

{

cout<< 错误打开源\ n;

返回1;

}

cout<< 请输入目标文件的名称:\ n";

cin>>目的地;

while(FileName == Destination)

{

cout<< 源和目标文件名必须不同

请尝试另一个:\ n" ;;

cin>>目的地;

}

ofstream out(目的地,ios :: binary | ios :: out);

if(!out)

{

cout<< 创建目标文件时出错\ n;

返回0;

}

char c;

while(in.get(c))

{

if(c!= CR)

{

out.put(c);

}

}

in.close();

out.close ();

返回0;

}

int helpfile()

{

// system(" pause");

cout<< \ n \ nnHere is help \ n \ nn;

ifstream inf(help,ios :: in);

if( !inf)

{

cout<< 读取文件的错误\ n;

返回1;

}

string theLine ="" ;;

while(getline(inf,theLine))

{

cout<< theLine<<结束;

}

inf.close();

返回0;

}
< br $> b $ b -

我不是真正的人类,但我在地球上玩一个。
http://zentara.net/japh.html




" kalio80" < KA ***** @ hotmail.com>在消息中写道

news:98 ************************** @ posting.google.c om ...

"kalio80" <ka*****@hotmail.com> wrote in message
news:98**************************@posting.google.c om...
大家好我想创建一个文件,将文本文件从
unix转换为windows和windows到unix
我理解它的一般概念为
unix使用换行LF
Windows使用CRLF carrige返回和馈线
我的程序会提示用户输入文件打开然后
提示输入目标文件以保存新的格式化文件。
我几乎没有问题,我一直试图解决几个小时
Hi everyone I am trying to create a file that converts text files from
unix to windows and windows to unix
I understand the general concept of it as
unix uses line feed LF
Windows uses CRLF carrige return and feed line
my program will prompt the user to enter a file to open and then
prompts for a destination file to save the new formatted file.
I am having few problems that i have been trying to solve for few
hours




您可以使用Boost Iostreams库中的换行过滤器:

http://home.comcast.net/~jturkanis/i.../?path=5.9.2.2

该库将成为1.33版本的一部分,是可在此处获得:

http://home.comcast.net/~jturkanis/iostreams/

最诚挚的问候,

Jonathan Turkanis



You can use the newline filter from the Boost Iostreams library:

http://home.comcast.net/~jturkanis/i.../?path=5.9.2.2
The library will be part of the 1.33 release and is available here:

http://home.comcast.net/~jturkanis/iostreams/

Best Regards,
Jonathan Turkanis


ka ***** @ hotmail .com (kalio80)在留言新闻中写道:< 98 ************************** @ posting.google。 com> ...
ka*****@hotmail.com (kalio80) wrote in message news:<98**************************@posting.google. com>...
大家好我想创建一个文件,将文本文件从
unix转换为windows和windows到unix


[...]

我几乎没有问题,我一直试图解决几个小时
这里是我创建的代码
Hi everyone I am trying to create a file that converts text files from
unix to windows and windows to unix
[ ... ]
I am having few problems that i have been trying to solve for few
hours
here is the code I created




如果你告诉我们那些问题是什么,我们通常可以帮助你更好地解决问题。稍微查看一下你的代码,我看到一个数字

的问题。他们中的大多数都是次要的,但在整个代码中都是不断重复的。首先也是最明显的,你的代码似乎假设

标准库在全局命名空间中,当它实际上在

std命名空间中时。这可能反映了编译器中的问题。


其次,我认为你将代码分解为函数的方式可以改进 - b $ b可以改进 - - 例如,请注意uw()和wu()都包含

(基本相同)代码来检索输入的名称和

输出文件。


你的代码还包含一些错误的假设:虽然它确实是/>
UNIX不需要CR来表示文件的结尾,它'在UNIX下,CR有时也包含在文本文件中,这也是正确的。如果他们发生了b $ b,你可能想要不受阻碍地通过它们。


鉴于这本质上是一个过滤器,我认为它是互动的

是一个错误 - 我会让用户传递输入文件,输出文件,

并在命令行上完成转换。像这样的程序坚持交互通常很快就会很烦人。实际上,

来了,我可能也会将功能分成两个单独的程序,每个程序转换成一个转换的方向

它们更容易使用。


有了这些,我会编写如下代码:


// wu .c

#include< stdio.h>

#include< stdlib.h>


int main(int argc,char ** argv){

int c;

FILE * infile,* outfile;


if(argc! = 3){

fprintf(stderr," Usage:uw< infile>< outfile> \ n");

返回EXIT_FAILURE;

}


if(NULL ==(infile = fopen(argv [1]," rb"))){

fprintf( stderr,无法打开输入文件。\ n;)

返回EXIT_FAILURE;

}


if( NULL ==(outfile = fopen(argv [2]," wb"))){

fprintf(stderr,无法创建输出文件。\ n );

返回EXIT_FAILURE;

}


while(EOF!=(c = fgetc(infile)))

if(c!=''\ n'')

fputc(c,outfile);


fclose(infile) ;

fclose(outfile);

返回0;

}


uw.c会是相同的,除了内环看起来像

之类的东西:


while(EOF!=(c = fgetc(infile))){

if(c ==''\ n'')

fputc(''\ r'',outfile);

fputc (c,outfile);

}


我已经使用过CI / O操作符 - 对于这个任务,我认为没有优势/>
iostreams。


如果我打算在一个程序中执行这两个操作,我会有一个

常用函数打开文件,然后wu()和uw()只会

从一个文件转换到另一个文件。如果你真的想以这种方式做事,那我仍然会让用户看起来像两个单独的

程序。具体来说,我会创建两个单独的硬链接到相同的

可执行文件,并在可执行文件中查看argv [0]以查看

它被调用的名称适当的。


-

后来,

杰瑞。


宇宙是自己想象的虚构。



We can usually help solve problems better if you tell us what those
problems ARE. Looking through your code a little bit, I see a number
of problems. Most of them are minor, but repeated freuently throughout
the code. First and most obvious, your code seems to assume that the
standard library is in the global namespace, when it''s actually in the
std namespace. This may reflect a problem in your compiler.

Second, I think the way you''ve split up the code up into functions
could be improved -- for example, note that both uw() and wu() contain
(essentially identical) code to retrieve the names of the input and
output files.

Your code also contains some false assumptions: while it''s true that
UNIX doesn''t require a CR to signal the end of a file, it''s also true
that CRs are sometimes included in text files under UNIX. Where they
occur, you probably want to pass them through unhindered.

Given that this is essentially a filter, I think making it interactive
is a mistake -- I''d have the user pass the input file, output file,
and conversion to be done on the command line. Programs like this that
insist on interaction generally get tiresome very quickly. Actually,
come to that, I''d probably also separate the functionality out into
two separate programs, one for each direction of conversion to make
them easier to use.

With those given, I''d write the code something like this:

// wu.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
int c;
FILE *infile, *outfile;

if (argc != 3) {
fprintf(stderr, "Usage: uw <infile> <outfile>\n");
return EXIT_FAILURE;
}

if (NULL == (infile=fopen(argv[1], "rb"))) {
fprintf(stderr, "Unable to open input file.\n");
return EXIT_FAILURE;
}

if (NULL == (outfile=fopen(argv[2], "wb"))) {
fprintf(stderr, "Unable to create output file.\n");
return EXIT_FAILURE;
}

while (EOF != (c=fgetc(infile)))
if ( c != ''\n'')
fputc(c, outfile);

fclose(infile);
fclose(outfile);
return 0;
}

uw.c would be the same, except that the inner loop would look
something like:

while ( EOF != (c=fgetc(infile))) {
if ( c == ''\n'')
fputc(''\r'', outfile);
fputc(c, outfile);
}

I''ve used the C I/O operators -- for this task, I see no advantage to
iostreams.

If I was going to do both operations in a single program, I''d have a
common function for opening files, and then wu() and uw() would ONLY
do the conversion from one file to another. If you really want to do
things this way, I''d still make it look to the user like two separate
programs. Specifically, I''d create two separate hard links to the same
executable file, and inside the executable look at argv[0] to see
which name it was invoked under and act appropriately.

--
Later,
Jerry.

The universe is a figment of its own imagination.


这篇关于c ++转换文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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