Search code examples
c++arraysascii

arrange line in txt file in ASCII order using array and display them


#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!


Solution

  • 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.