为什么C代码在我给出/a.out后会暂停? #包括 #包括 #包括 //该程序是一个读取数字序列的排序应用程序 //并在屏幕上打印。从这里读取文件, //是一个回调函数。 typedef int(*CompFunc)(常量字符*,常量字符*); typedef int(*ReadCheck)(字符nullcheck); 字符数组[100]; //让这个函数在库本身中完成。它不在乎什么 //比较函数的位置及其实现方式。意义假设 //函数希望按升序或降序进行排序 //然后,必须通过“比较”函数中的客户机代码进行更改 //谁将实现lib代码。 void ReadFile(文件*fp,ReadCheck rc) { 字符a; chard[100]; int count=0,count1=0; a=fgetc(fp); 而(1!=(*rc)(a)) {if(a=='\0') { //d[count1]='\0'; strcpy(&数组[count],d); 计数=计数+1; } 其他的 { d[count1]=a; count1=count1+1; } } } void Bubblesort(字符*数组,整数大小,整数元素大小,CompFunc cf) {int i,j; 内部*温度; 对于(i=0;i
只是可能程序运行需要一点时间…只是可能为什么C代码在我给出/a.out后会暂停? #包括 #包括 #包括 //该程序是一个读取数字序列的排序应用程序 //并在屏幕上打印。从这里读取文件, //是一个回调函数。 typedef int(*CompFunc)(常量字符*,常量字符*); typedef int(*ReadCheck)(字符nullcheck); 字符数组[100]; //让这个函数在库本身中完成。它不在乎什么 //比较函数的位置及其实现方式。意义假设 //函数希望按升序或降序进行排序 //然后,必须通过“比较”函数中的客户机代码进行更改 //谁将实现lib代码。 void ReadFile(文件*fp,ReadCheck rc) { 字符a; chard[100]; int count=0,count1=0; a=fgetc(fp); 而(1!=(*rc)(a)) {if(a=='\0') { //d[count1]='\0'; strcpy(&数组[count],d); 计数=计数+1; } 其他的 { d[count1]=a; count1=count1+1; } } } void Bubblesort(字符*数组,整数大小,整数元素大小,CompFunc cf) {int i,j; 内部*温度; 对于(i=0;i,c,C,只是可能程序运行需要一点时间…只是可能 这里有一个可能的问题:当fgetc返回EOF(-1)时会发生什么情况?而(1!=(*rc)(a))问题似乎出在哪里?您是否尝试过使用调试器查看它被卡住的地方?不,它没有运行它。它没有终止。很可能是一个无限循环。我认为这不是非常关键的时间…查看代码,似乎只有5个元素需要排序。@mekasperasky:更新答案。 #include<stdlib.h> #include<stdio.h> #include<string.h>
这里有一个可能的问题:当fgetc返回EOF(-1)时会发生什么情况?
而(1!=(*rc)(a))
问题似乎出在哪里?您是否尝试过使用调试器查看它被卡住的地方?不,它没有运行它。它没有终止。很可能是一个无限循环。我认为这不是非常关键的时间…查看代码,似乎只有5个元素需要排序。@mekasperasky:更新答案。
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
//This program is a sorting application that reads a sequence of numbers
//from a file and prints them on the screen . The reading from the file here,
//is a call back function .
typedef int (*CompFunc)(const char* , const char* );
typedef int (*ReadCheck)(char nullcheck);
char array[100];
//Let this function be done in the library itself . It doesn't care as to
//where the compare function and how is it implemented . Meaning suppose
//the function wants to do sort in ascending order or in descending order
//then the changes have to be done by the client code in the "COMPARE" function
//who will be implementing the lib code .
void ReadFile(FILE *fp,ReadCheck rc)
{
char a;
char d[100];
int count = 0,count1=0;
a=fgetc(fp);
while(1 != (*rc)(a))
{ if(a=='\0')
{
//d[count1]='\0';
strcpy(&array[count],d);
count=count+1;
}
else
{
d[count1]=a;
count1=count1+1;
}
}
}
void Bubblesort(char* array , int size , int elem_size , CompFunc cf)
{ int i,j;
int *temp;
for( i=0;i < size ;i++)
{
for ( j=0;j < size -1 ; j++)
{
// make the callback to the comparision function
if(1 == (*cf)(array+j*elem_size,array+ (j+1)*elem_size))
{
//interchanging of elements
temp = malloc(sizeof(int *) * elem_size);
memcpy(temp , array+j*elem_size,elem_size);
memcpy(array+j*elem_size,array+(j+1)*elem_size,elem_size);
memcpy(array + (j+1)*elem_size , temp , elem_size);
free(temp);
}
}
}
}
//Let these functions be done at the client side
int Compare(const char* el1 , const char* el2)
{
int element1 = *(int*)el1;
int element2 = *(int*)el2;
if(element1 < element2 )
return -1;
if(element1 > element2)
return 1 ;
return 0;
}
int ReadChecked(char nullcheck)
{
if (nullcheck=='\n')
return 1;
else
return 0;
}
int main()
{
FILE *fp1;
int k;
fp1=fopen("readdata.txt","r");
ReadFile(fp1,&ReadChecked);
Bubblesort((char*)array,5,sizeof(array[0]),&Compare);
printf("after sorting \n");
for (k=0;k<5;k++)
printf("%d",array[k]);
return 0;
}