我如何使我的循环转到char*阵列的尽头,并将每个城市都放在链接的列表中(城市被划分为白色的空间)

How do I make my for loop go to the end of the char* array and put every city in a linked list(The citys are divided by a whitespace)

本文关键字:城市 链接 列表 空间 白色 划分 循环 我的 何使 char 尽头      更新时间:2023-10-16

因此,在我的作业中,我需要按第一个字母按字母顺序对城市进行排序,如果有更多的城市带有相同的起始信,然后以相反的顺序输出。

我已经设法将输入从文件中获取到缓冲区char数组中。但是,当我尝试对其进行排序(通过数组不起作用(

#include <iostream>
#include <fstream>
using namespace std;
class List
{
    struct Node
    {
        char* input;
        Node* next;
    };
    Node* head;
    Node* tail;
public:
    List()
    {
        head = NULL;
        tail = NULL;
    }
    void createnode(char* city)
    {
        Node* temp = new Node;
        temp->input= city;
        temp->next = NULL;
        if (head == NULL)
        {
            head = temp;
            tail = temp;
        }
        else
        {
            Node* point = new Node;
            point->input= city;
            point->next = head;
            head = point;
        }
    }
    void display()
    {
        Node* point = head;
        if (point == NULL)
        {
            cout << endl << "====================================" << endl << "List Doesnt exist/is deleted" << endl << "====================================" << endl;
            return;
        }
        cout << endl << "your list:" << endl;
        while (point != NULL)
        {
            cout << point->input<< "t";
            point = point->next;
        }
        cout << endl;
    }
};
int main()
{
  ////////////////THE PART WHERE I EXTRACT INFORMATION FROM THE INPUT FILE
    ifstream file("paldies.in", ifstream::binary);
    fstream file2;
    file2.open("paldies.out", ios::out);
    if (!file)
    {
        cout << "Error desune!";
        return 0;
    }
    file.seekg(0, file.end);
    int length = file.tellg();
    file.seekg(0, file.beg);
    char * buffer = new char[length];
    cout << "Reading " << length << " characters....." << endl;
    file.read(buffer, length);
    if (length == 0)
    {
        char nothing[8] = "Nothing";
        file2.write(reinterpret_cast<char*>(nothing), 8 * sizeof(char));
        file.close();
        file2.close();
        return 0;
    }
    if (file)
    {
        cout << "all characters read succesfully.";
    }
    else
    {
        cout << "error: only " << file.gcount() << " could be read";
    }
    file.close();
////////////////////////////THIS IS THE PART THATS NOT WORKING FOR ME
    List a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
    for (buffer; *buffer != ''; buffer++)
    {
        if (buffer[0] == 's')
        {
        char s_begining[255] = "";
            for (int i = 0; buffer[0] != ' '; i++)
            {
                s_begining[i] = buffer[0];
                buffer++;
            }
            s.createnode(s_benining);
        }
        buffer++;
    }
cout << endl<<buffer<<endl;

    s.display();
    file2.close();
    return 0;
}

输入:斯普林菲尔德费城底特律克利夫兰迈阿密丹佛斯普林菲尔德·西雅图·杰克逊维尔

正确的输出:克利夫兰丹佛底特律杰克逊维尔迈阿密费城西雅图斯普林菲尔德斯普林菲尔德

实际输出:由于我只是用字母测试字母,它仅作为Springfield出现,如果我将char s_begining声明更改为if语句或循环外部,则会给出不同的结果。

我觉得问题在第一个循环中的某个位置,因为当我将其取出时,第一个元素就可以进入列表,但是当我放回有时会出现异常时,什么也没有发生(空列表(,或列表中有4个输入,其中还包含垃圾数据。

另外,如果我删除缓冲区 ;在第一个循环结束时,它也会破坏东西。

到目前为止,我已经正确地在列表中找到了1个城市名称,那就是第一个(Springfield(。

编辑:我忘了提到我只能使用fstream库,一切都必须由我自己编码!

使用STL可以实现很多您要做的事情:

#include <algorithm>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>
int main() {
    // 0. Set up variables
    std::ifstream inFile("pladies.in");
    std::ofstream outFile("pladies.out");
    std::vector<std::string> cities;
    // 1. Read each line of the input file to a vector
    std::string line;
    while (std::getline(inFile, line)) {
        cities.push_back(line);
    }
    // 2. Sort the vector alphabetically
    std::sort(cities.begin(), cities.end());
    // 3. Write the vector to the output file
    std::copy(cities.begin(), cities.end(), std::ostream_iterator<std::string>(outFile, "n"));
}

( repl.it (