gpt4 book ai didi

c - C语言对字符串和文件的快速排序

转载 作者:行者123 更新时间:2023-11-30 15:19:35 25 4
gpt4 key购买 nike

我对文件中字符串的快速排序实现有疑问。

本练习需要输入要打开的文件的文件名,最多可以有 100 个项目。使用函数 char fill(FILE* myfile),文件中包含的每一行都会通过 while 循环复制到数组中,该循环在文件末尾结束。

然后调用函数 sort(strings, start, end, leq) 对字符串数组进行排序,在函数结束时,该函数将被复制到一个名为 sorted_myfile

我的问题是:如何测试我的程序是否可以处理文件?

在函数char filling中,声明leq_fn leq指的是指向函数typedef bool(* leq_fn )(char *, char * ) 包含在快速排序的函数中。我希望我为您提供了帮助我所需的所有信息。谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "quicksort.h"
#define MAX_LENGTH 100
#define EXTRA_LENGTH 101
#define LENGTH_STRING 255

char filling(FILE *myfile);
char *strings[MAX_LENGTH]; //Array of at most 100 items

char filling(FILE *myfile){
char *row;
char new_filename[LENGTH_STRING];
FILE* sorted_myfile;
int i=0, start=0, end=MAX_LENGTH-1;
bool res_cmplen, res_cmpalpha, res_cmpalpha_nocase;
FILE *sorted_myfile;

while(i<=EOF){
if(i==EXTRA_LENGTH){
printf("The file has more than 100 items\n");
}else{
row = calloc(LENGTH_STRING, sizeof(char));
fgets(row, LENGTH_STRING, myfile);
strings[i]=row;
i++;
free(row);
}
}
leq_fn leq;

leq = cmp_len;
res_cmplen = (*leq)("hello", "bye");
leq = cmp_alpha;
res_cmpalpha = (*leq)("HELLO", "bye");
leq = cmp_alpha_nocase;
res_cmpalpha_nocase = (*leq)("hello", "BYE");

sort(strings, start, end, leq);
scanf("%s", new_filename);
sorted_myfile = fopen(new_filename, "w");
for(i=0;i<end;i++){
fputs(strings[i], sorted_myfile);
}

return 0;
}


int main(void) {
char filename[LENGTH_STRING];
FILE *myfile;

printf("What is the file name?\n");
scanf("%s", filename);
myfile = fopen(filename, "r");

if(ferror(myfile)!=0){
printf("The file doesn't exist\n");
}else{
filling(myfile);
}

return 0;
}

最佳答案

我首先从修复这些问题开始:

  1. char *row; 更改为 char *row = NULL;
  2. 同样将 FILE*sorted_myfile; 更改为 FILE*sorted_myfile = NULL;
  3. 更改bool res_cmplen、res_cmpalpha、res_cmpalpha_nocase;
    bool res_cmplen = 0、res_cmpalpha = 0、res_cmpalpha_nocase = 0;
    bool res_cmplen = 1,res_cmpalpha = 1,res_cmpalpha_nocase = 1;
    取决于实现起来更安全。
  4. FILE *sorted_myfile; 更改为 FILE *sorted_myfile = NULL;
  5. main() 中将 FILE *myfile; 更改为 FILE *myfile = NULL;
  6. free(row); row 成为悬空指针,然后按原样使用它,然后在循环的最后一次迭代中,将其保留为是,这会调用未定义的行为。因此,释放内存后执行 row = NULL;
  7. char *strings[MAX_LENGTH]; 不是 //最多 100 项的数组,而是 //MAX_LENGTH 指针的数组。这里会发生什么:strings[i]=row;
  8. 在下面的代码片段中:

    while(i<=EOF){ 
    if(i==EXTRA_LENGTH){
    printf("The file has more than 100 items\n");
    }else{
    row = calloc(LENGTH_STRING, sizeof(char));
    fgets(row, LENGTH_STRING, myfile);
    strings[i]=row;
    i++;
    free(row);
    }
    }

    ielse{} 条件下递增,并且 if(i==EXTRA_LENGTH) 计算结果为 true,即 if i 等于 EXTRA_LENGTH,它不会增加或修改,因为这只发生在 else{} 中。因此,当i == 255时,您的代码将永远不会进入else{}条件,并且只会无限打印“The file has more than 100 item”

  9. 这是一个小问题,但是,在 main() 中,我可以看到 myfile = fopen(filename, "r");,这将打开文件。美好的。虽然我看不到你什么时候关闭它,如果你关闭的话。可能在某个地方缺少 fclose();

关于c - C语言对字符串和文件的快速排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30571680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com