c - C中的冒泡排序文件

标签 c windows file casting bubble-sort

我编写了一个连接到服务器并从中接收代码行的程序,然后将所有代码行打印到一个文本文件中,问题是,服务器发送的所有代码行都不按顺序,我的意思是是在包含代码行的文本文件中没有顺序,它可以是第 55 行,然后是第 33 行,我正在尝试编写一个函数来对文件进行排序,以便代码行按顺序排列,我知道我需要使用冒泡排序并将字符串中的行号转换为 int,但我以前从未尝试过对文本文件进行冒泡排序,这是我的代码:(忽略注释)

#define  _WINSOCK_DEPRECATED_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<winsock2.h>
#include<windows.h>
#include<string.h>


#define LEN 1024

void sortcode(FILE *fp);
int main(void)
{
    FILE *fp;
    fp = fopen("theCode.txt", "wt");
    int i;
    WSADATA info;
    char str[LEN];
    str[LEN - 1] = NULL;
    char str2[LEN];
    str2[LEN - 1] = NULL;
    char temp[8] = "5000000"; // the row number
    int j = strlen(temp) - 1;// the index of the temp string
    int k = 0;
    int err;
    err = WSAStartup(MAKEWORD(2, 0), &info);
    if (err != 0)
    {
        printf("WSAStartup failed with error: %d\n", err);
        exit(1);
    }
    int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (s == INVALID_SOCKET)
    {
        printf("Error creating socket = %d\n", WSAGetLastError());
    }
    else
    {
        printf("Socket function succeeded\n");
    }
    struct sockaddr_in clientService;
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr("54.152.161.133");
    clientService.sin_port = htons(6714);
    int cResult = connect(s, (struct socketaddr*)&clientService, sizeof(clientService));
    if (cResult == SOCKET_ERROR)
    {
        printf("Connect function failed with error: %d\n", WSAGetLastError());
        cResult = closesocket(cResult);
        if (cResult == SOCKET_ERROR)
        {
            printf("Close socket function closed with an error: %1d\n", WSAGetLastError());
        }
        WSACleanup();
        //return 1;
    }
    //Until this part, it's all taken from the slideshow.
    send(s, "100", LEN, 0); //Sending code 100: Requesting to connect.
    printf("Request to connect was sent using 100\n");
    recv(s, str, LEN, 0); //Recieving a code to the string str.
    printf("Code recieved: %s\n", str);
    if (strcmp("101", str) == 0)
    {
        printf("Connection was successful\n");
    }
    else
    {
        printf("The connection failed\n");
    }
    send(s, "400", LEN, 0); //Sending a request for the number of code lines.
    printf("Request for the amount of code lines was sent using 400\n");
    recv(s, str, LEN, 0); //Recieving the answer on str, you'll get code 401+The number of lines for example 4010079.
    printf("String recieved: %s\n", str);
    printf("The amount of code lines: 0079\n");
    printf("%s", str);
    for (k = 1; k <= 7; k++)
    {
        for (i = 0; i <= 9; i++)
        {
            temp[j] = i + 0x30;
            send(s, temp, LEN, 0);
            recv(s, str, LEN, 0);
            fprintf(fp, str);
            fprintf(fp, "\n");
        }
        temp[j - 1] = k + 0x30;
        temp[j] = 0 + 0x30;
    }
    //You need to add the part with the files where you print all the lines including the code in them to a txt file.
    //Good Luck, first try to solve that i to string conversion.
    system("PAUSE");
    return (0);
}
void sortcode(FILE *fp)
{
    int i, j, k;
    char str2[LEN];
    fp = fopen("theCode.c", "rt");
    for (i = 0; i < 79; i++)
    {
        for (j = 3; j < 7; j++)
        {

        }
    }
}

最佳答案

即使您决定编写自己的排序函数而不是使用 qsort(),冒泡排序算法对于少数项目来说也是一个非常糟糕的选择。

如果您愿意在内存中缓冲所有行,直到您读取所有行并对其进行排序(这可能是您最好的选择),那么您应该考虑边做边排序。每次你阅读一个新行时,在已经阅读过的行中找到它的位置,并将其插入那里。这本质上是一种插入排序。

如果您希望您的行大部分按顺序排列,则从后到前线性搜索每个插入点。这是标准的插入排序,对于近似有序的输入,它表现得非常好。对于没有特定顺序的大量行,您可以考虑使用二进制搜索来查找每个插入点。

如果您使用链表来保存线条(如果您事先不知道会有多少行,这是一个合理的选择),那么二分搜索替代方案可以很好地扩展到大输入。

关于c - C中的冒泡排序文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31078000/

相关文章:

android - 将 cout 格式的数据写入 printf

c++ - 删除目录及其中的所有子目录

c++ - 输入: int fd = open ("file");?时fd代表什么

file - 如何在 Go 中的 io.Reader 上测试 EOF?

android - 如何使用 FileInputStream 设置文件名?

c - 我的系统是("clear");不清除所有内容?

c - strlen() 拒绝从 struct hostent 读取字符串 *

windows - 使窗口只运行一个用户可见的程序

c - 如何禁用 clang 的 gnu 扩展?

C - 在 while 循环中直接写入数组,同时使用 recv 接收数据