使用数组以ASCII顺序排列TXT文件并显示它们

发布于 2025-01-23 17:51:38 字数 1177 浏览 1 评论 0原文

#include <stdio.h>
#include <string.h>
#include <fstream>
#include <iostream>

using namespace std;

int main() {
    ifstream infile;          // ifstream is reading file
    infile.open("read.txt");  // read.txt is the file we need to read
    std::cout << infile;
    string str;
    if (infile.is_open()) {
        while (getline(infile, str)) {
            char str[2000], ch;
            int i, j, len;
            len = strlen(str);
            for (i = 0; i < len; i++) {
                for (j = 0; j < (len - 1); j++) {
                    if (str[j] > str[j + 1]) {
                        ch = str[j];
                        str[j] = str[j + 1];
                        str[j + 1] = ch;
                    }
                }
            }
        }
        cout << "\nProcessed data:" << str;
    }

    return 0;
}

我的txt文件:

Today is a fine day.
It’s sunny.
Let us go out now!

我的结果应该是:

    .Taaaddefiinosyy
 ’.Innsstuy
    !Legnooosttuuw

在这里也考虑空间。 我是C ++的新手。 我需要一些专业人士帮助。 非常感谢!

#include <stdio.h>
#include <string.h>
#include <fstream>
#include <iostream>

using namespace std;

int main() {
    ifstream infile;          // ifstream is reading file
    infile.open("read.txt");  // read.txt is the file we need to read
    std::cout << infile;
    string str;
    if (infile.is_open()) {
        while (getline(infile, str)) {
            char str[2000], ch;
            int i, j, len;
            len = strlen(str);
            for (i = 0; i < len; i++) {
                for (j = 0; j < (len - 1); j++) {
                    if (str[j] > str[j + 1]) {
                        ch = str[j];
                        str[j] = str[j + 1];
                        str[j + 1] = ch;
                    }
                }
            }
        }
        cout << "\nProcessed data:" << str;
    }

    return 0;
}

My txt file:

Today is a fine day.
It’s sunny.
Let us go out now!

My result should be:

    .Taaaddefiinosyy
 ’.Innsstuy
    !Legnooosttuuw

Spaces is consider here as well.
I'm new to C++.
I need some pros help.
Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

装纯掩盖桑 2025-01-30 17:51:38

利用stl:

  • 逐行读取您的文件,读取std :: String使用std :: getline
  • 使用std :: Ranges :: Sort对每行进行对。
  • 打印。

下面的示例:

  • 还使用fmt库代替std :: Cout,并
  • std :: istringstream而不是std :: ifstream 。

[demo]

#include <algorithm>  // sort
#include <fmt/core.h>
#include <sstream>  // istringstream
#include <string>  // getline

int main() {
    std::istringstream iss{
        "Today is a fine day.\n"
        "It's sunny.\n"
        "Let us go out now!\n"
    };

    fmt::print("Original file:\n{}\n", iss.str());

    fmt::print("Processed file:\n");
    std::string line{};
    while (std::getline(iss, line)) {
        std::ranges::sort(line);
        fmt::print("{}\n", line);
    }
}


// Outputs:
//
//   Original file:
//   Today is a fine day.
//   It's sunny.
//   Let us go out now!
//   
//   Processed file:
//       .Taaaddefiinosyy
//    '.Innsstuy
//       !Legnooosttuuw

Making use of the STL:

  • Read your file line by line into a std::string using std::getline.
  • Sort every line using std::ranges::sort.
  • Print it.

The example below:

  • also uses the fmt library instead of std::cout, and
  • reads from a std::istringstream instead of a std::ifstream.

[Demo]

#include <algorithm>  // sort
#include <fmt/core.h>
#include <sstream>  // istringstream
#include <string>  // getline

int main() {
    std::istringstream iss{
        "Today is a fine day.\n"
        "It's sunny.\n"
        "Let us go out now!\n"
    };

    fmt::print("Original file:\n{}\n", iss.str());

    fmt::print("Processed file:\n");
    std::string line{};
    while (std::getline(iss, line)) {
        std::ranges::sort(line);
        fmt::print("{}\n", line);
    }
}


// Outputs:
//
//   Original file:
//   Today is a fine day.
//   It's sunny.
//   Let us go out now!
//   
//   Processed file:
//       .Taaaddefiinosyy
//    '.Innsstuy
//       !Legnooosttuuw
圈圈圆圆圈圈 2025-01-30 17:51:38

您的代码不起作用,因为:

  1. line std :: cout&lt;&lt; ifile;是错误的。如果要打印 istream :: operator bool() 为了确定文件是否成功打开,您应该写std :: cout&lt;&lt; infile.operator bool();std :: cout&lt;&lt; static_cast&lt; bool&gt;(infile);而不是。但是,最好简单地编写std :: cout&lt;&lt; infile.fail();std :: cout&lt;&lt; !infile.fail();
  2. 功能 std :: strlen 作为参数,指向有效字符串的指针。也许您打算写str.length()?在这种情况下,您应该删除声明char str [2000],因为它会阴影声明string str;
  3. 排序后,您应该在被下一行覆盖之前立即打印出排序的结果。当前,您仅在程序结束时一次打印内容str,因此您仅打印最后一行。

执行上述修复程序后,您的代码应该看起来像这样:

#include <stdio.h>
#include <string.h>
#include <fstream>
#include <iostream>

using namespace std;

int main() {
    ifstream infile;          // ifstream is reading file
    infile.open("read.txt");  // read.txt is the file we need to read
    std::cout << infile.fail();
    string str;
    if (infile.is_open()) {
        while (getline(infile, str)) {
            char ch;
            int i, j, len;
            len = str.length();
            for (i = 0; i < len; i++) {
                for (j = 0; j < (len - 1); j++) {
                    if (str[j] > str[j + 1]) {
                        ch = str[j];
                        str[j] = str[j + 1];
                        str[j + 1] = ch;
                    }
                }
            }

            cout << "\nProcessed data:" << str;
        }
    }

    return 0;
}

对于输入,

Today is a fine day.
It's sunny.
Let us go out now!

该程序具有以下输出:

0
Processed data:    .Taaaddefiinosyy
Processed data: '.Innsstuy
Processed data:    !Legnooosttuuw

请注意,您的问题中发布的输入包含一个forward tick '而不是postrophe '。这可能会导致麻烦。例如,当我测试您的程序时,将此tick编码为多字节UTF-8字符,因为它在7位 us-ascii 。这导致您的排序算法失败,因为它仅支持单字节字符。我只能通过用输入中的撇号替换向前刻度来修复此错误。

Your code does not work, because:

  1. The line std::cout << infile; is wrong. If you want to print the result of istream::operator bool() in order to determine whether the file was successfully opened, then you should write std::cout << infile.operator bool(); or std::cout << static_cast<bool>(infile); instead. However, it would probably be better to simply write std::cout << infile.fail(); or std::cout << !infile.fail();.
  2. The function std::strlen requires as a parameter a pointer to a valid string. Maybe you intended to write str.length()? In that case, you should delete the declaration char str[2000], because it shadows the declaration string str;.
  3. You should print the sorted result immediately after sorting it, before it gets overwritten by the next line. Currently you are only printing the content str a single time at the end of your program, so you are only printing the last line.

After performing the fixes mentioned above, your code should look like this:

#include <stdio.h>
#include <string.h>
#include <fstream>
#include <iostream>

using namespace std;

int main() {
    ifstream infile;          // ifstream is reading file
    infile.open("read.txt");  // read.txt is the file we need to read
    std::cout << infile.fail();
    string str;
    if (infile.is_open()) {
        while (getline(infile, str)) {
            char ch;
            int i, j, len;
            len = str.length();
            for (i = 0; i < len; i++) {
                for (j = 0; j < (len - 1); j++) {
                    if (str[j] > str[j + 1]) {
                        ch = str[j];
                        str[j] = str[j + 1];
                        str[j + 1] = ch;
                    }
                }
            }

            cout << "\nProcessed data:" << str;
        }
    }

    return 0;
}

For the input

Today is a fine day.
It's sunny.
Let us go out now!

this program has the following output:

0
Processed data:    .Taaaddefiinosyy
Processed data: '.Innsstuy
Processed data:    !Legnooosttuuw

Note that the input posted in your question contains a forward tick instead of an apostrophe '. This could cause trouble. For example, when I tested your program, this forward tick was encoded into a multi-byte UTF-8 character, because it is not representable in 7-bit US-ASCII. This caused your sorting algorithm to fail, because it only supports single-byte characters. I was only able to fix this bug by replacing the forward tick with an apostrophe in the input.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文