gpt4 book ai didi

c++ - 如何将文本文件从 Windows 转换为 Unix

转载 作者:太空狗 更新时间:2023-10-29 19:59:54 24 4
gpt4 key购买 nike

从 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

最佳答案

*如果*您只需要转换简单的 ascii(可能还有 utf-8)文本文件,您可以在翻译模式下循环逐行读取源文件(在这种情况下为您处理足够的换行符)使用非成员 getline() 然后将行输出到输出文件,同时在除最后一行之外的每行之后插入\n 或\r\n。

然后,您可以删除原始文件并将临时文件重命名为原始文件的名称。或者,如果您愿意,您可以将这些行 push_back 到 vector 中。然后,您可以关闭文件的输入句柄,执行 ofstream out("filename", ios_base::trunc) 并将 vector 的元素写入文件,同时用您想要的换行符分隔它们。

这完全取决于您的要求。

以下是一个错误处理最少的示例。但是,我想在这里展示的只是 FOR 循环和逐行阅读,作为一种不同的做事方式。

convert_file.exe "test.txt""linux"

convert_file.exe "test.txt""赢"

#include <iostream>
#include <string>
#include <fstream>
#include <ostream>
#include <cstdlib>
#include <cstdio>
using namespace std;

int main(int argc, char* argv[]) {
if (argc != 3) {
cerr << "Usage: this.exe file_to_convert newline_format(\"linux\" or \"win\")" << endl;
return EXIT_FAILURE;
}
string fmt(argv[2]);
if (fmt != "linux" && fmt != "win") {
cerr << "Invalid newline format specified" << endl;
return EXIT_FAILURE;
}
ifstream in(argv[1]);
if (!in) {
cerr << "Error reading test.txt" << endl;
return EXIT_FAILURE;
}
string tmp(argv[1]);
tmp += "converted";
ofstream out(tmp.c_str(), ios_base::binary);
if (!out) {
cerr << "Error writing " << tmp << endl;
return EXIT_FAILURE;
}
bool first = true;
for (string line; getline(in, line); ) {
if (!first) {
if (fmt == "linux") {
out << "\n";
} else {
out << "\r\n";
}
}
out << line;
first = false;
}
in.close();
out.close();
if (remove(argv[1]) != 0) {
cerr << "Error deleting " << argv[1] << endl;
return EXIT_FAILURE;
}
if (rename(tmp.c_str(), argv[1]) != 0) {
cerr << "Error renaming " << tmp << " to " << argv[1] << endl;
return EXIT_FAILURE;
}
}

不过正如其他人所说,已经有一些实用程序(包括像 Notepadd++ 这样的文本编辑器)可以为您进行换行符转换。因此,除非出于其他原因(您未指定)这样做,否则您不需要自己实现任何事情。

关于c++ - 如何将文本文件从 Windows 转换为 Unix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10069038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com