【发布时间】:2015-09-13 16:36:48
【问题描述】:
我编写了一个程序,它连接到服务器并从中接收代码行,然后将所有代码行打印到文本文件中,问题是,服务器不按顺序发送所有代码行,我的意思是是在包含代码行的文本文件中没有顺序,它可以是第 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()函数?在许多方面,学习如何使用标准排序比编写自己的排序更好。 -
Bubble Sort Homework 的可能重复项
-
OT:在
#include后面加一个空格不再流行了吗?我已经看到很多 C 代码发布到 SO 中省略了空格,我不知道为什么。 -
@SeanBright:风格不好;标准没有这样做,程序员也不应该这样做。是的,这很常见——令人沮丧的常见。但这并不明智。
-
“我知道我需要使用冒泡排序” - 不,冒泡排序几乎不合适 - 只需使用 C 库中的
qsort。
标签: c windows file casting bubble-sort