写一段程序C语言实现读取记事本中每个diff2:后面的数字并且排序好
时间: 2023-04-02 20:05:19 浏览: 312
以下是实现读取记事本中每个diff2:后面的数字并且排序好的 C 语言程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 1024
int main() {
FILE *fp;
char line[MAX_LINE_LENGTH];
int nums[MAX_LINE_LENGTH], count = ;
fp = fopen("notes.txt", "r");
if (fp == NULL) {
printf("Failed to open file\n");
exit(1);
}
while (fgets(line, MAX_LINE_LENGTH, fp) != NULL) {
char *p = strstr(line, "diff2:");
if (p != NULL) {
int num = atoi(p + strlen("diff2:"));
nums[count++] = num;
}
}
fclose(fp);
// Sort the numbers
for (int i = ; i < count - 1; i++) {
for (int j = i + 1; j < count; j++) {
if (nums[i] > nums[j]) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
// Print the sorted numbers
for (int i = ; i < count; i++) {
printf("%d ", nums[i]);
}
printf("\n");
return ;
}
阅读全文