linux中win文件转为unix,如何将文本文件从Windows转换为Unix

从Unix转换到Windows时,我得到正确的输出;但是,从Windows到Unix时,我得到了一些奇怪的输出。我认为我必须允许的是删除回车'\ r'。虽然这不起作用。当我运行代码后打开文本文件时,我得到了一些奇怪的结果,第一行是正确的,然后所有的地狱破坏。

int main( )

{

bool windows = false;

char source[256];

char destination[256]; // Allocate the max amount of space for the filenames.

cout << "Please enter the name of the source file: ";

cin >> source;

ifstream fin( source, ios::binary );

if ( !fin ) // Check to make sure the source file exists.

{

cerr << "File " << source << " not found!";

getch();

return 1;

}//endif

cout << "Please enter the name of the destination file: ";

cin >> destination;

ifstream fest( destination );

if ( fest ) // Check to see if the destination file already exists.

{

cout << "The file " << destination << " already exists!" << endl;

cout << "If you would like to truncate the data, please enter 'Y', "

<< "otherwise enter 'N' to quit: ";

char answer = char( getch() );

if ( answer == 'n' || answer == 'N' )

{

return 1;

}//endif

}//endif

clrscr(); // Clear screen for neatness.

ofstream fout( destination, ios::binary );

if ( !fout.good() ) // Check to see if the destination file can be edited.

{

cout << destination << "could not be opened!" << endl;

getch();

return 1;

}//endif

// Open the destination file in binary mode.

fout.open( destination, ios::binary );

char ch = fin.get(); // Set ch to the first char in the source file.

while ( !fin.eof() )

{

if ( ch == '\x0D' ) // If ch is a carriage return, then the source file

{ // must be in a windows format.

windows = true;

}//endif

if ( windows == true )

{

ch = fin.get(); // Advance ch, so that at the bottom of the loop, the

}//endif // carriage return is not coppied into the new file.

if ( windows == false )

{

if ( ch == '\x0A' ) // If the file is in the Unix format..

{

fout.put( '\x0D' ); // When a new line is found, output a carriage

}//endif // return.

}//endif

fout.put( ch );

ch = fin.get();

}//endwh

if ( windows == true )

{

fout.put( '\x0A' );

}//endif

fout.close();

fin.close(); // Close yer files.

if ( windows == true ) // A little output for user-friendly-ness.

{

cout << "The contents of " << source << " have been coppied to "

<< destination << " and converted to Unix format." << endl;

}else{

cout << "The contents of " << source << " have been coppied to "

<< destination << " and converted to Windows format." << endl;

}//endif

cout << "Enter any key to quit.." << endl;

getch();

return 0;

}//endmn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值