使用数组以ASCII顺序排列TXT文件并显示它们
#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
利用stl:
std :: String
使用std :: getline
。std :: Ranges :: Sort
对每行进行对。下面的示例:
fmt
库代替std :: Cout
,并std :: istringstream
而不是std :: ifstream 。
[demo]
Making use of the STL:
std::string
usingstd::getline
.std::ranges::sort
.The example below:
fmt
library instead ofstd::cout
, andstd::istringstream
instead of astd::ifstream
.[Demo]
您的代码不起作用,因为:
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();
。std :: strlen
作为参数,指向有效字符串的指针。也许您打算写str.length()
?在这种情况下,您应该删除声明char str [2000]
,因为它会阴影声明string str;
。str
,因此您仅打印最后一行。执行上述修复程序后,您的代码应该看起来像这样:
对于输入,
该程序具有以下输出:
请注意,您的问题中发布的输入包含一个forward tick
'
而不是postrophe'
。这可能会导致麻烦。例如,当我测试您的程序时,将此tick编码为多字节UTF-8字符,因为它在7位 us-ascii 。这导致您的排序算法失败,因为它仅支持单字节字符。我只能通过用输入中的撇号替换向前刻度来修复此错误。Your code does not work, because:
std::cout << infile;
is wrong. If you want to print the result ofistream::operator bool()
in order to determine whether the file was successfully opened, then you should writestd::cout << infile.operator bool();
orstd::cout << static_cast<bool>(infile);
instead. However, it would probably be better to simply writestd::cout << infile.fail();
orstd::cout << !infile.fail();
.std::strlen
requires as a parameter a pointer to a valid string. Maybe you intended to writestr.length()
? In that case, you should delete the declarationchar str[2000]
, because it shadows the declarationstring str;
.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:
For the input
this program has the following output:
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.