需要帮助将数据文件转换为特定输出

Need help converting data file to specific output

本文关键字:输出 转换 文件 帮助 数据      更新时间:2023-10-16

我有一个名为:dataFile(无扩展名)的文件,其中包含以下行:

CALCULATOR
Lamp . Post
aBc - deF

我的程序需要像这样输出这个数据文件:

calculator
lamppost
abcdef

因此,它基本上应该尊重'\n',将所有字符更改为小写并删除任何非字母字符。。。

这是我到目前为止的代码,

#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
int main()
{
char file;
ifstream cin;
cin.open("dataFile");
cin.get(file);
while (!cin.eof())
    {
    file = tolower(file);                 // Convert all letters to capital letters
         if (('A'<= file && 'Z' >= file)) // Restrict to only letters
            {
             cout << file;                // Output file
            }
    cin.clear();
    cin.get(file);
    }
return 0;
}

我面临的问题是程序的输出看起来像这样:

计算器lampostabcdef

我应该如何修改我的代码,使其分别输出每一行?

如果你可以使用其他东西,这应该是:

bash $ (tr '[A-Z]' '[a-z]' | tr -d -c '[:alpha:]n') < dataFile

否则,这可能就是你想要的:

#include <cctype>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
    char c;
    ifstream file("dataFile");
    while (!file.eof()) {
            c = file.get();
            if (isalpha(c) || c == 'n')
                    cout << tolower(c);
    }
    return 0;
}